Wazy
Wazy

Reputation: 494

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.

Is this code working properly?

enter image description here

Upvotes: 1

Views: 312

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

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')

Upvotes: 1

Related Questions