[Fixed]-Django Multiple File Upload

40đź‘Ť

You should access multipart values with getlist, i.e.:

for afile in request.FILES.getlist('files'):
    File(file=afile, files=test).save()

I don’t think it’s getting the list as a python list when you use request.FILES['files'].

Also, if you want to use the HTML5 multiple file upload instead of many file forms, take a look here: django form with multiple file fields

👤Edd

3đź‘Ť

I haven’t done this exact thing before, but it seems like you’d need to do some processing on the actual audio file before saving it.

The general structure would be:

if form.is_valid():
    object = form.save(commit=False)
    t = handle_uploaded_file(request.FILES['file'])
    object.field.save(t[0], t[1])

And in the handle_uploaded_file, you’d probably need to use something like ffmpeg to process the audio and then return (filename, content) to your main function.

In addition, using .chunks would be on the actual file passed:

str=""
for c in request.FILES['file'].chunks(): 
   str += c
👤David542

0đź‘Ť

In addition to handling the file-array in the request-object correctly as pointed out in the other posts, you should also make sure that in the html input, you have a “multiple”-attribute that is set to true.
Example:

<input name="file_field" multiple="true" required="false" id="id_file_field" data-enpass.usermodified="yes" type="file">
👤user3061675

Leave a comment