Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django multiple files in one input can't be read by server

When I submit to Django a multiple files upload form

<input type="file" name="files" multiple />

I get a sensible result in request.FILES:

(MultiValueDict: {u'files': [(InMemoryUploadedFile: 0202.jpg (image/jpeg)), (InMemoryUploadedFile: 0203.jpg (image/jpeg))]})

But then my confusion starts. I thought request.FILES['files'] would contain a couple of files (appears to be a list), but it shows only

0203.jpg

No InMemoryUploadedFile part and more importantly: only the last file!

Looping through request.FILES with .iteritems() only goes over that one file too, request.FILES['files'] is distinctly uniterable; the below code actually froze up the console and made it beep endlessly:

for v in request.FILES['files']:
    print v
    print type(v)

So... Is any of this this normal? What am I doing wrong?

As I'm starting to think this may be a bug: I'm using Django 1.4.2 with Python 2.7 on Windows 7.

like image 586
Mark Avatar asked Dec 02 '12 02:12

Mark


1 Answers

It turns out the answer was on Stack Overflow after all, on a slightly different (client side, at least) problem here: multiple files upload using same input name in django

Turns out that request.FILES['files'] was wrong and should have been request.FILES.getlist('file').

Why this is the case is a complete mystery to me. If anyone can answer with an explanation, I'll gladly switch the accepted answer.

like image 131
Mark Avatar answered Oct 24 '22 14:10

Mark