move_uploaded_file 函数相当于 django 中的函数

发布于 2024-10-02 02:45:16 字数 1607 浏览 1 评论 0原文

我想将一些文件从表单上传到云服务器而不重定向到那里。所以我发现 this 使用 php/ajax 的教程,但是那里使用了 django 中不存在的函数 - move_uploaded_file。我怎样才能用 django 达到同样的效果?目前我正在使用 django-filetransfers 的一部分,但是在提交表单后,省略了 if request.method == POST 之后的整个部分:

def upload_handler(request):            
    if request.method == 'POST':
        form = ArtifactSubmissionForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
        return HttpResponseRedirect('/')
    else:    
        upload_url, upload_data = prepare_upload(request, "uploadlink")
        form = ArtifactSubmissionForm()    

    myfileid = create_myfileid()
    return direct_to_template(request, 'rte/artifact_inline.html',
        {'upload_url': upload_url,
        'form': form,
        'upload_data': upload_data,
        'myfileid': myfileid,
        'artifact': artifact,
        'submissions': submissions})

并且 html:

{% load filetransfers %}

{% block artifact %}
<h1>Submit</h1>
<form action="{{ upload_url }}" method="POST" enctype="multipart/form-data">
    {% render_upload_data upload_data %}
    <table>{{ form }}</table>    
    <p>
        <input type="hidden" maxlength="64" name="myfileid" value="{{ myfileid }}" >
    </p>
    <p>
        <input type="submit" value="Submit" />
    </p>
</form>
{% endblock %}

编辑:

我只需要发送文件到服务器进行进一步处理,然后从服务器响应中读取它们的 url。不需要将它们用作 File 对象。

I would like to upload some files from form to cloud server without redirecting there. So I've found this tutorial with php/ajax but a function that is not present in django is used there - move_uploaded_file. How can I achieve the same with django ? Currently I'm using a part of django-filetransfers, but after submitting my form the whole part after if request.method == POST is omitted :

def upload_handler(request):            
    if request.method == 'POST':
        form = ArtifactSubmissionForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
        return HttpResponseRedirect('/')
    else:    
        upload_url, upload_data = prepare_upload(request, "uploadlink")
        form = ArtifactSubmissionForm()    

    myfileid = create_myfileid()
    return direct_to_template(request, 'rte/artifact_inline.html',
        {'upload_url': upload_url,
        'form': form,
        'upload_data': upload_data,
        'myfileid': myfileid,
        'artifact': artifact,
        'submissions': submissions})

and the html:

{% load filetransfers %}

{% block artifact %}
<h1>Submit</h1>
<form action="{{ upload_url }}" method="POST" enctype="multipart/form-data">
    {% render_upload_data upload_data %}
    <table>{{ form }}</table>    
    <p>
        <input type="hidden" maxlength="64" name="myfileid" value="{{ myfileid }}" >
    </p>
    <p>
        <input type="submit" value="Submit" />
    </p>
</form>
{% endblock %}

EDIT:

I just need to send files to server for further processing and then read their urls from servers response. Don't need to use them as File objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2024-10-09 02:45:16

django-storages 插件具有允许您自动将上传的内容存储到您选择的存储库的功能。它需要链接到您的 MEDIA_URL,这很烦人,但这只是代码,您可以解决它。

源代码可以在这里找到:Django 存储

我建议您在网络上寻找您喜欢的网络。如果您使用 Amazon Cloudfront(就像我目前的位置一样),那么使用在签名 URL 上禁用 HTTPS 的 Cloudfront 可以为我们每次下载节省几分钱,随着时间的推移,这确实会增加。

The django-storages plug-in has features that allows you to automatically store uploaded content to the repository of your choice. It has an annoying need to be linked to your MEDIA_URL, but that's just code and you can work around it.

Source code can be found here: Django storages.

I recommend rooting around the network for one that you like. If you're using Amazon Cloudfront, as I do at my current position, using one that disabls HTTPS over signed URLS saved us a few millicents per download, which does add up over time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文