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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chandan Gupta
  • 1,410
  • 2
  • 13
  • 29

1 Answers1

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)
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • is above "if file and allowed_file...." loop going to be inside of the for loop you mentioned here? – Chandan Gupta Jul 23 '13 at 06:00
  • @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. – Sean Vieira Jul 23 '13 at 06:01
  • it says `NameError: global name 'handle_file' is not defined`. what module do I need to import `import ` to get getlist and request.files? – Chandan Gupta Jul 23 '13 at 06:09
  • @ChandanGupta - yes - that is true - you will need to create that function or replace the fake call with your saving logic ;-) – Sean Vieira Jul 23 '13 at 06:10
  • And you don't need to import anything else to have access to `request.files.getlist('some_name')`. – Sean Vieira Jul 23 '13 at 06:14
  • I tried it. But its only taking one upload. Please have a look at the changed route above I have done in the question code. – Chandan Gupta Jul 23 '13 at 06:23
  • I have added one more function which was creating problem. function wasn't called outside and hence last value of "filename" was taken and therefore only single upload. Now by calling that "upload" function inside for loop, it saves the each file to uplaods folder in its iteration. Cheers! Thanks [SeanVieira](http://stackoverflow.com/users/135978/sean-vieira) – Chandan Gupta Jul 23 '13 at 09:53