I'm uploading multiple files to django and when I was saved files only it saved one. Execution example:
------------------------------------ start files ------------------------
<MultiValueDict: {u'patientFiles': [<InMemoryUploadedFile: Registro VFC JOSE DE LA CRUZ_16_03_2015.txt (text/plain)>, <InMemoryUploadedFile: JDTD datos vfc_27_04_2015.txt (text/plain)>]}>
*-1-*Registro VFC JOSE DE LA CRUZ_16_03_2015.txt
*-2-*<django.core.files.storage.FileSystemStorage object at 0xaf731aac>
*-1-*JDTD datos vfc_27_04_2015.txt
*-2-*<django.core.files.storage.FileSystemStorage object at 0xaf731e4c>
*-3-*JDTD datos vfc_27_04_2015.txt
*-4-*JDTD%20datos%20vfc_27_04_2015.txt
--------------------------------------------------------------------------
How you can see it goes to 3 and 4 points one time and it doesn't save first file. Have you any idea about?
Thanks and next are code.
This is django-server code:
def startFiles(self,request):
print colored("----------------- start files -----------------------",'blue')
print colored(request.FILES,'blue')
for afile in request.FILES.getlist('patientFiles'):
myfile = afile
print colored("*-1-*" + str(myfile),'blue')
fs = FileSystemStorage()
print colored("*-2-*" + str(fs),'blue')
filename = fs.save(myfile.name, myfile)
print colored("*-3-*" + str(filename),'blue')
uploaded_file_url = fs.url(filename)
print colored("*-4-*" + str(uploaded_file_url),'blue')
print colored("--------------------------------------------------------------------------------",'blue')
this is HTML code:
<form id="SuPF" class="form-group" enctype="multipart/form-data" action="/formSingupPatient" method="POST">
<input type="file" multiple="true" name="patientFiles">
<button type="submit" id="SubmitForm" class="btn btn-default">Submit</button>
</form>
finaly I used python's files API to save many files. I change startFiles code with:
def startFiles(self,request):
print colored("----------------- start files -----------------------",'blue')
print colored(request.FILES,'blue')
for afile in request.FILES.getlist('patientFiles'):
myfile = afile
print colored("*-1-*" + str(myfile),'blue')
fs = FileSystemStorage()
print colored("*-2-*" + str(fs),'blue')
filename = settings.MEDIA_ROOT + "/" + myfile.name
f = open(filename,'w')
print colored(f,'blue')
f.write(myfile.read())
f.close()
print colored("--------------------------------------------------------------------------------",'blue')
I used buffer to create a file and save file.
settings.MEDIA_ROOT is a system path where save files.
I hope help you on future.
To upload to model and to save in file system as well I used this
for d in request.FILES.getlist("asd"):
def process(f):
with open('./media/file_' + x.name, 'wb+') as destination:
for chunks in f.chunks():
destination.write(chunks)
obj = Details()
obj.xxx= './media/f_' + x.name
obj.type = type
obj.save()
process(d)