I'm trying to upload file using uploadhandler in Djan开发者_运维问答go. But it's returning the error:
You cannot alter upload handlers after the upload has been processed
Code:
def upload_form(request):
if request.method == 'POST':
outPath = '/opt/workspace/jup2/juppro/uploads/23232'
if not os.path.exists(outPath):
os.makedirs(outPath)
request.upload_handlers.insert(0, ProgressUploadHandler(request, outPath)) # place our custom upload in first position
upload_file = request.FILES.get('file', None) # start the upload
return HttpResponse("uploaded ok")
What's wrong with that code?
You have to define the uploadhandler before you start uploading. The moment you can access request.POST the file has allready been uploaded to the memory or a temporary file. This makes defining an uploadhandler pointless, as the upload has allready been finished.
Django docs are quite clear about when to define a custom uploadhandler: "You can only modify upload handlers before accessing request.POST or request.FILES -- it doesn't make sense to change upload handlers after upload handling has already started." Without knowing enough about your code i can only guess, but i think it should be enough to modify your code to the following:
def upload_form(request):
outPath = '/opt/workspace/jup2/juppro/uploads/23232'
if not os.path.exists(outPath):
os.makedirs(outPath)
request.upload_handlers.insert(0, ProgressUploadHandler(request, outPath)) # place our custom upload in first position
if request.method == 'POST':
upload_file = request.FILES.get('file', None) # start the upload
return HttpResponse("uploaded ok")
精彩评论