Created
December 5, 2011 16:50
-
-
Save tkh44/1434275 to your computer and use it in GitHub Desktop.
ajax_upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ajax_upload( request ): | |
if request.method == "POST": | |
if request.is_ajax( ): | |
# the file is stored raw in the request | |
upload = request | |
is_raw = True | |
is_common = False | |
# AJAX Upload will pass the filename in the querystring if it is the "advanced" ajax upload | |
try: | |
filename = request.GET[ 'qqfile' ] | |
except KeyError: | |
return HttpResponseBadRequest( "AJAX request not valid" ) | |
# not an ajax upload, so it was the "basic" iframe version with submission via form | |
else: | |
is_raw = False | |
if len( request.FILES ) == 1: | |
# FILES is a dictionary in Django but Ajax Upload gives the uploaded file an | |
# ID based on a random number, so it cannot be guessed here in the code. | |
# Rather than editing Ajax Upload to pass the ID in the querystring, | |
# observer that each upload is a separate request, | |
# so FILES should only have one entry. | |
# Thus, we can just grab the first (and only) value in the dict. | |
upload = request.FILES.values( )[ 0 ] | |
else: | |
raise Http404( "Bad Upload" ) | |
filename = upload.name | |
# save the file | |
success = save_upload( upload, filename, is_raw ) | |
# let Ajax Upload know whether we saved it or not | |
import json | |
ret_json = { 'success': success, } | |
return HttpResponse( json.dumps( ret_json ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment