Chandan Gupta Chandan Gupta - 1 year ago 101
Python Question

e Multiple file upload with flask built in uplaoder

Uploading Files in flask has been explained in the docs properly. But I was wondering if the same built-in flask file upload can be used for multiple uploads.
I went through this answer but I couldn't get it done. It says "flask" not defined. Not sure if I am missing some modules to import or just I don't know to use method

of flask.request.files .

My form looks like:

<form action="" method=post enctype=multipart/form-data>
<input type=file name="file[]" multiple>
<input type=submit value=Upload>

and the route is like:

@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
files = request.files.getlist['file[]']
for file in files:
if file and allowed_file(file.filename):
#filename = secure_filename(file.filename)
upload(filename)['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',

if I replace file with "file[]" will it work? I can select multiple files doing so but flask accepts and prints only one file selected and uploads only one. Looks like I am missing something silly here.


I edited above route with suggestion below.


One more function was necessary to keep the filename iterating and saving it.

def upload(filename):
filename = 'https://localhost/uploads/' + filename

and calling this function inside for loop above.
Did the job!

Not sure if its a genuine solution but it did the trick.

Answer Source

You'll want to call the getlist method of request.files (which is an instance of werkzeug.datastructures.MultiDict):

files = request.files.getlist('file')
for file in files:

handle_file might be implemented like this:

def handle_file(f):
    if not allowed_file(f.filename):

    filename = secure_filename(f.filename)['UPLOAD_FOLDER'], filename)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download