4

I'm trying to upload file using uploadhandler in Django. 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?

1 Answer 1

1

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")
Sign up to request clarification or add additional context in comments.

2 Comments

@CpILL: This answer has been given in 2011, most probably for Django 1.4 or even 1.3
yeah, i figured it out. The CSRF middleware was accessing it before my view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.