How do I upload multiple files with Django?
2 Answers
After a lot of pain I eventually got uploadify (http://www.uploadify.com/) working with django, but the problem wasn't really django's, but getting it to work with Apple Mac's; browsers on that platform don't serve the cookies from within Flash; you need to set them manually:
So I include these in my render-to-reponse:
return render_to_response('files_upload.html', {
'session_cookie_name': settings.SESSION_COOKIE_NAME,
'session_key': request.session.session_key
And present them from uploadify via the configuration laid down in the template:
$(document).ready(function() {
$('#fileInput').uploadify({
'scriptData': {'{{session_cookie_name}}': '{{session_key}}'},
I've seen this done better with a decorator over the view, but this was the dirty hack I put in middleware to copy the POST into COOKIE before the session middleware runs to restore the session.
class FakeUploadCookieMiddleware(object):
"""TODO: replace the hardcoded url '/upload' with a 'reverse'."""
def process_request(self, request):
if request.path == '/upload/' \
and request.POST.has_key(settings.SESSION_COOKIE_NAME):
request.COOKIES[settings.SESSION_COOKIE_NAME] = \
request.POST[settings.SESSION_COOKIE_NAME]
logging.debug('Faking the session cookie for an upload: %s', \
request.POST[settings.SESSION_COOKIE_NAME])

- 50,179
- 34
- 152
- 186
-
well, now this should definitely be placed somewhere on uploadify home page, struggled for some time with, til gave it up and used http://blogs.bigfish.tv/adam/2009/06/14/swfupload-jquery-plugin/ (not with django though, but still, definitely cookies must have been a problem) – Misha Reyzlin May 31 '10 at 22:46
-
1Did exactly what you have here and it still does not work - still error 403. Any ideas? – miki725 Jul 18 '11 at 19:34
Someone has already created a multi-upload field that might serve your purposes.
http://scompt.com/archives/2007/11/03/multiple-file-uploads-in-django
Django has great support for building forms and working with file uploads. I would read through these articles to better understand how the multi-upload field code works.

- 87,612
- 17
- 125
- 175

- 14,660
- 17
- 61
- 66
-
Thanks. This seems to work, but I get a funky error. No matter what I upload I get: "No file was submitted. Check the encoding type on the form." Looking at the source, I can see that it's a TypeError being raised. – Aug 28 '09 at 20:55
-
3
-
1This is only 'multiple' in the sense that he's creating many `` fields. Would be better to support HTML5's `multiple` attribute on a ``. – Jonatan Littke May 02 '12 at 08:15