Flask parse multiple file upload
Assume you have a form with:
This strategy can be often use in Angular >2. Angular Material does not support input type file, so to enable file upload we catch click mouse event on usual material input control and trigger click on hidden input type file. If you would like to upload multiple files with this strategy into Flask server, it will be hold in ImmutableMultiDict(more). This dictionary can store multiple values for the same key. For example:
If you would try to get values by the key, you will get only the first value.
To get all values you need to use getlist('<key>')
<input type="file" .... multiple>
This strategy can be often use in Angular >2. Angular Material does not support input type file, so to enable file upload we catch click mouse event on usual material input control and trigger click on hidden input type file. If you would like to upload multiple files with this strategy into Flask server, it will be hold in ImmutableMultiDict(more). This dictionary can store multiple values for the same key. For example:
ImmutableMultiDict([('files', <FileStorage: '[Edward_Thorp]_Beat_the_marke.pdf' ('application/pdf')>), ('files', <FileStorage: '[Edward_Thorp]_The_mathematics_of_gambling.djvu' ('image/vnd.djvu')>), ('files', <FileStorage: ['Larry_Dressler]_Roger_Schwarz_Standing_in_the_F.pdf' ('application/pdf')>)])
If you would try to get values by the key, you will get only the first value.
To get all values you need to use getlist('<key>')
for f in received_files.getlist('files'):
print(f.filename)
Comments
Post a Comment