Uploading multiple files with Flask


Is there a way to receive multiple uploaded files with Flask? I’ve tried the following:

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

And then printed the contents of request.files['file']:

@app.route('/upload', methods=['POST'])
def upload():
  if not _upload_dir:
    raise ValueError('Uploads are disabled.')

  uploaded_file = flask.request.files['file']
  print uploaded_file
  media.add_for_upload(uploaded_file, _upload_dir)
  return flask.redirect(flask.url_for('_main'))

If I upload multiple files, it only prints the first file in the set:

<FileStorage: u'test_file.mp3' ('audio/mp3')>  

Is there a way to receive multiple files using Flask’s built-in upload handling? Thanks for any help!


You can use method getlist of flask.request.files, for example:

@app.route("/upload", methods=["POST"])
def upload():
    uploaded_files = flask.request.files.getlist("file[]")
    print uploaded_files
    return ""

Answered By – Fedor Gogolev

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published