4

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 getlist 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>
</form> 

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)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return redirect(url_for('uploaded_file',
                                filename=filename))

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.

=====EDITED======

I edited above route with suggestion below.

=====ADDITION====

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.

2
  • files = request.files['file[]'] should be files = request.files.getlist('file[]') Commented Jul 23, 2013 at 6:54
  • that was just a typo. I have updated it. Commented Jul 23, 2013 at 7:54

1 Answer 1

7

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:
    hande_file(file)

handle_file might be implemented like this:

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

    filename = secure_filename(f.filename)
    f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)
Sign up to request clarification or add additional context in comments.

7 Comments

is above "if file and allowed_file...." loop going to be inside of the for loop you mentioned here?
@ChandanGupta - yes. handle_file would have all of the logic in your method up to, but not including the redirect (which would go outside of the for loop.
it says NameError: global name 'handle_file' is not defined. what module do I need to import import <what??> to get getlist and request.files?
@ChandanGupta - yes - that is true - you will need to create that function or replace the fake call with your saving logic ;-)
And you don't need to import anything else to have access to request.files.getlist('some_name').
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.