Upload multiple files or a whole folder through Flask
answer:
html file
<html> <body> <form action = "http://localhost:5000/uploader" method = "POST" enctype = "multipart/form-data"> <input type = "file" name = "file" multiple/> <input type = "submit"/> </form> </body></html>
in the input tag, multiple is a mandatory argument to allow access of multiple files!
flask code:
from flask import Flask, render_template, request#from werkzeug import secure_filenamefrom werkzeug.utils import secure_filenameapp = Flask(__name__)@app.route('/')def upload_file(): return render_template('upload.html')@app.route('/uploader', methods = ['GET', 'POST'])def upload_file1(): if request.method == 'POST': files = request.files.getlist("file") for file in files: file.save(secure_filename(file.filename)) return 'file uploaded successfully'if __name__ == '__main__': app.run(debug = True)
Use flask.request.getlist to get the list of files in the directory.To process multiple files just use a loop to manage them as shown above.