Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check size of multiple uploaded files

I have a feature that allows users to upload multiple files. I want to check the size of these files and dissallow files that are too large

images = request.FILES['images'].size

if images > settings.MAX_UPLOAD_SIZE:
    raise Exception(f'Image {images} is too large 3mb max')

I have been playing with this code but I cannot figure out a way to loop over all of the files.

What confuses me about this is i am not iterating over all the files but it still properly throws the exception no matter what order the file that is too large appears in.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is this code working properly?

enter image description here

>Solution :

You use .getlist(…) [Django-doc] to obtain the list of files:

for image in request.FILES.getlist('images'):
    if image.size > settings.MAX_UPLOAD_SIZE:
        raise Exception(f'Image {images} is too large 3mb max')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading