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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With