flask check if user selected a file to upload

I’ve checked on stackoverflow and other places and everywhere they use files.filename to check if a file is available or not. If I do this I get the message

AttributeError: ‘list’ object has no attribute ‘filename’

Do I miss something? Or is there another way to check if the user has selected a file to upload?

my HTML:

  <form method="POST" enctype="multipart/form-data" action="/upload">
    <label>Choose a problem report file (*.zip).
      <input type="file" name="file[]"  multiple="" accept=".zip">
      <input type="submit" value="Upload problem report(s)">

my python

import flask
from werkzeug.wrappers import request
import os
app = flask.Flask("upload")

UPLOAD_FOLDER = './upload'

def get_html(page_name):
    html_file = open(page_name + ".html")
    content = html_file.read()
    return content

@app.route("/upload", methods=["POST"])
def upload():
    files = flask.request.files.getlist("file[]")
    if files.filename !='':
        for file in files:
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
    return get_html("index")

It’s not the best code I know, but if I leave out the check for the filename the upload works.


It should be something like this:

files = flask.request.files.getlist("file[]")
for file in files:
    if file.filename !='':
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))

As the error message suggested, your get a list, and a list has not filename attribute, so you need to check the items in the list.