简体   繁体   中英

move_uploaded_file function equivalent in django

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM