Upload multiple files in Flask

Previously, I did not know how to upload multiple files with Flask, so I made a trial and error, so a memo of the result

Server-side processing


if request.files.getlist('upload_files')[0].filename:
    upload_files = request.files.getlist('upload_files')
    for upload_file in upload_files:
        #Tmp the received file/Save to
        upload_file.save("tmp/" + secure_filename(upload_file.filename))

Client-side HTML


<form method="post">
    <div class="form-group pull-left">
        <input type="file" id="upload_files" name="upload_files" multiple="multiple" class="form-control">
        <p class="help-block">* Multiple files can be specified</p>
    </div>
    <div class="form-group">
        <input type="submit" value="Send" class="form-control btn btn-primary">
    </div>
</from>

It was easy to specify multiple items on the client side and send them, but I was struggling because I was not sure how to receive them on the server side. .. .. As a result of various investigations, when multiple files are sent, it seems that they are stored in request.files with the same KEY, so it is solved by getting from request.files with "getlist ()" instead of "get ()".

Recommended Posts

Upload multiple files in Flask
Configure a module with multiple files in Django
Upload files with Django
Image uploader in Flask
Rsync multiple files at once
[Linux] Grep multiple gzip files in a directory at once
Search for strings in files
HTTP environment variables in Flask
Decompress multiple compressed files (Python)
Avoid multiple loops in Python
Embed wav files in Jupyter
Extract multiple list duplicates in Python
Transpose CSV files in Python Part 1
[Python] Show multiple windows in Tkinter
Let's upload S3 files with CLI
Create a CSV reader in Flask
Manipulate files and folders in Python
Fixed Apscheduler running twice in Flask
Handling of JSON files in Python
Use Flask to run external files
Upload & move GCS files with Go
About parameter processing in Flask request.args
Sort large text files in Python
Read files in parallel with Python
Statistical test (multiple test) in Python: scikit_posthocs
Bring files in Windows to WSL
Delete multiple elements in python list
Image upload function with Vue.js + Flask
Extract strings from files in Python