How to upload multiple files with flask-wtf?
from wtforms import MultipleFileField
from werkzeug.utils import secure_filename
class Upload_Form(FlaskForm):
files = MultipleFileField(render_kw={'multiple': True})
And the route
@app.route('/', methods=['GET', 'POST'])
def upload():
form = Upload_Form()
if form.validate_on_submit():
for file in form.files.data:
file_name = secure_filename(file.filename)
file.save(file_name)
Instead of using the FileField, use the MultipleFileField. It supports multiple files.
For example:
from wtforms import MultipleFileField
class NewFileForm(FlaskForm):
files = MultipleFileField('File(s) Upload')
Then to access the files:
@app.route('/', methods=['GET', 'POST'])
def upload():
form = NewFileForm()
if form.validate_on_submit():
files_filenames = []
for file in form.files.data:
file_filename = secure_filename(file.filename)
data.save(os.path.join(app.config['UPLOAD_FOLDER'], data_filename))
files_filenames.append(file_filename)
print(files_filenames)
return render_template('input_form.html', form=form)
return render_template('input_form.html', form=form)