12

I'm looking for a way to upload a file to s3. I am using django. I am currently using amazon's python library for uploading along with the following code:

View:

def submitpicture(request):
    fuser = request.session["login"]

    copied_data = request.POST.copy()
    copied_data.update(request.FILES)
    content_type = copied_data['file'].get('content-type')
    ffile = copied_data['file']['content']
    key = '%s-%s' % (fuser, ''.join(copied_data['file']['filename'].split(' ')))
    site_s3.save_s3_data(key, ffile, content_type)

Template:

<form action="/submitpicture/" method="POST">
    <input type="file" id="file" name="file" />
    <input type="submit" value="submit" />
</form>

However, when I actually try to run it i get the following error:

"Key 'file' not found in <QueryDict: {}>"
#MultiValueDictKeyError

I really don't see what I'm doing wrong. Can someone point me in the right direction?

Edit: Just in case someone wonders, I am planning on adding some validation after I get the actual upload working.

3 Answers 3

19

You will have to provide the enctype attribute to the FORM element (I've been bitten by this before). For example, your FORM tag should look like:

<form action="/submitpicture/" method="POST" enctype="multipart/form-data" >

Without the enctype, you will find yourself with an empty request.FILES.

Sign up to request clarification or add additional context in comments.

Comments

5

Instead of doing this manually I would take a look at the storage backend David Larlet has written for Django, django-storages

Comments

2

Adding enctype="multipart/form-data" seems like something that's worth mentioning in the "File Uploads" section of the django docs (http://docs.djangoproject.com/en/dev/topics/http/file-uploads/). Thoughts?

Comments

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.