开发者

move_uploaded_file function equivalent in django

开发者 https://www.devze.com 2023-01-25 12:56 出处:网络
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_uplo

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 us开发者_C百科ing 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消