I was wondering how to set a validation method which limits the size of each file when using the flask wtform MultipleFileField before then being able to save the file.
There was a useful post here, which describes exactly what I want to do but it is for a single file upload, but I can figure out how to make this work with the MultipleFileField as this seems to return a list if strings and therefore the .read() method which is used in the other post to get the size of the file does not work
Any help would be appreciated, and let me know if you need any more details, thanks!
One way to do this is by iterating through the list of files, and then applying the size check as shown in your linked post.
class SubmitMultipleFiles(FlaskForm): file = MultipleFileField('Files') submit = SubmitField('Submit') def validate_file(self, field): for file in field.data: if len(file.read()) > 2*1024: raise ValidationError('This file is too large.') file.seek(0)
I’ve put this into a custom validator, which checks each file and raises an error if one of them is too large.
Answered By – Patrick Yoder