E Multiple flask file uploads created in uplaoder

Loading files in a flash is explained in the documents correctly. But I was wondering if it is possible to use the same built-in flash file upload for multiple downloads. I went through this answer , but I could not do it. It states that the "bulb" is not defined. Not sure if I am missing some modules for import or I just don’t know if to use the getlist method for flask.request.files.

My form looks like this:

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

and the route is as follows:

 @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 replaced the file with "file []", will it work? I can select several files, but the checkbox only accepts and prints one selected file and uploads only one. It seems like I'm missing something stupid.

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

I edited the route above with the sentence below.

===== Addition ====

Another function was needed to save the iteration of the file and save it.

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

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

Not sure if this was a genuine solution, but it was a trick.

+4
source share
1 answer

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

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

handle_file can be implemented as follows:

 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) 
+7
source

Source: https://habr.com/ru/post/1492861/


All Articles