multiple files upload using same input name in django
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.
Given your url points to envia you could manage multiple files like this:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from django.http import HttpResponseRedirect
def envia(request):
for f in request.FILES.getlist('file'):
handle_uploaded_file(f)
return HttpResponseRedirect('/bulk/')
def handle_uploaded_file(f):
destination = open('/tmp/upload/%s'%f.name, 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()