S
S
sportik1742021-12-30 11:20:42
Django
sportik174, 2021-12-30 11:20:42

How to implement multiple file upload via AJAX in Django?

Now I'm loading files via AJAX like this and everything works fine (if there is only one file):

def user_docs_upload(request, user_id):
    user = User.objects.get(id=user_id)
    user_doc_add = UserDoc(
        user = user,
        file_link = request.FILES['files']
    )
    user_doc_add.save()


How to do the same only if there are several uploaded files in one input?

If you print like this:
print(request.FILES)

It turns out this:
<MultiValueDict: {'files': [<InMemoryUploadedFile: image_2021-12-23_16-56-48.png (image/png)>, <InMemoryUploadedFile: image_2021-12-28_15-34-44.png (image/png)>, <InMemoryUploadedFile: image_2021-12-28_15-40-54.png (image/png)>, <InMemoryUploadedFile: image_2021-12-28_15-48-09.png (image/png)>]}>


When trying to loop through:request.FILES['files']
for el in request.FILES['files']:
        print(el)


It turns out something like this:
b'\\\xa1\xf3\x8b\x82\xbf\xf5\x8c\x82}\n'
b'(\xa0\x80\xfe\x82\xc2~\xd53\n'
b'\xf6\xe9\x19\x05\xfb\x14\xf0E\xa2\xe0o=\xa3`\x9f\xbe\x08\xe8\x06\xeb\xa6\x08\xc1p\x06\xe8\x1dSU\xd5\xf3<I\[email protected]"\xd0q\x1c\xccq}\xd7u=\xcfs\x1c\xc70\x0c\xdb\xb6Y\x96\x95$\xa9\x07ud\x90j\x06\[email protected]\x9a\xa6q\x1c\x01;\t\xf4\xabM\xd3\xb4,\x0b\x94\[email protected]\x9f\xec\x0b{\xf2\xcf\x07\x05/\xef\x19\x05\xfb\xf4\x8cBV\xe8\xfc\xa2\xe0o=\xa3`\x9f\x02\n'
b"(\xa0\xbf\xa0\xb0_\xf5\x8c\x82}zF\xc1>\x05|\x91(\xf8[\xcf(\xd8\xa7/\x82\xa2(\x0cC +\xe4\xfb\xc8u=\xc7q`\x0e;\x8e\xe3\x14EA\xef\x18\xd0\x88H\[email protected]\xf6\x9fa(\x84Pw\xeaH\x14\xc5O}w\x18;\xd2\x9dv\xf2}\xdf\xb6=\x98\xefn\x18\xc6\xe9\xb1\\\xb8\xe7y\x18F\xb0,\xdb_&\x86\x9e\x19\xe7\xfa\xf9/4F\\\xc1>=\xe3\xf3\xf6\xff\x82={F\xc1>=\xe3B\xb3O\x01\x05\x14\xd0wP\xd8\xafzF\xc1>=\xa3`\x9f\x02\xbeH\x14\xfc\xadg\x14\xec\xd3\x17aY\x16\xfc\x003\xc4m\xdb\xb6m\x1b\x1a\[email protected]\x08!\x04\x03\xc8p\x1c\xc7t\xc3\x82\x11\xaa0\x9a\x0e\x98>\xbe\xefg2\x99O}w\x98\xde\x07\xfa\xd5\xbe\xef\xc3|V\x18[\x06c\xff(\x8a\xeaN-\xc1\x8c\xd5/\xea\xc1?'\x14r\x9f=\xa3`\x9f\x9eQ\xe0\n"
b'\x9d_\x14\xfc\xadg\x14\[email protected]\x01\x05\xf4\x17\x14\xf6\xab\x9eQ\xb0O\xcf(\xd8\xa7\x80/\x12\x05\x7f\xeb\x19\x05\xfb\[email protected]#\x17A\x10\x04A\xc0\xf8x\x82 \x80%DQ\x14\xc8C\xc3k0\x0c\xc3L\xcb\xc1q\x1c\xb2G\x14E\xd14\xe9\xfbH\xd3\xf43\xcd C\xa7\x87\x99\x01!\xc8u]\x82 `0\x19\xcb\xb2\xae\xebZ\x96\x05\x7f\x15\xc30\x82\xc0\x0b\x93\xe9\xbf\xec(\xd8\xa7g\x14\xb2B\xe7\x17\x05\x7f\xeb\x19\x05\xfb\[email protected]\x01\xfd\x05\x85\xfd\xaag\x14\xec\xd33\n'
b'\xf6)\xe0\x8bD\xc1\xdfzF\xc1>}\x11\xdd\xe9\x1a\x10\x99\xc6?\x01\xa0\xf5|\xf2\xc5\x98a\xda\xbd\xf49\x0b(\xa0\x80\x02\n'
b'(\xa0\x80\x02\n'
b'(\xa0\x80\x02\n'
b'(\xa0\x80\x02\n'
b'\xe85\xf4w\xd1\x9f\x02\n'
b'(\xa0\x80\x02\n'
b'(\xa0\x80\x02\n'
b'(\xa0\x80\x02\n'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sportik174, 2021-12-30
@sportik174

figured out instead

for el in request.FILES['files']:
        print(el)

Need to write
for el in request.FILES.getlist('files'):
        print(el)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question