Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple files upload using same input name in django

i m having trouble in uploading multiple files with same input name:

<input type=file name="file"> <input type=file name="file"> <input type=file name="file"> 

at django side

print request.FILES :  <MultiValueDict: {u'file': [ <TemporaryUploadedFile: captcha_bg.jpg (image/jpeg)>, <TemporaryUploadedFile: 001_using_git_with_django.mov (video/quicktime)>, <TemporaryUploadedFile: ejabberd-ust.odt (application/vnd.oasis.opendocument.text)> ]}> 

so all three files are under single request.FILES['file'] object . how do i handle for each files uploaded from here?

like image 694
Abu Aqil Avatar asked May 12 '09 06:05

Abu Aqil


People also ask

Does Python Django support multiple file upload?

Here is a quick example of how to add a multiple file form to your Django application. Most multiple-upload front-ends were created without Django in mind, so interfacing with tools is not always straightforward as it might be with a different language.

Which input property allows you to select many files at once?

The multiple attribute is a boolean attribute. When present, it specifies that the user is allowed to enter more than one value in the <input> element. Note: The multiple attribute works with the following input types: email, and file.


1 Answers

for f in request.FILES.getlist('file'):     # do something with the file f... 

EDIT: I know this was an old answer, but I came across it just now and have edited the answer to actually be correct. It was previously suggesting that you could iterate directly over request.FILES['file']. To access all items in a MultiValueDict, you use .getlist('file'). Using just ['file'] will only return the last data value it finds for that key.

like image 160
Justin Voss Avatar answered Sep 20 '22 23:09

Justin Voss