How To Upload Multiple Files With Flask-wtf?
I am trying to upload multiple files with flask-wtf. I can upload a single file with no problems and I've been able to change the html tag to accept multiple files but as of yet I
Solution 1:
Instead of using the FileField, use the MultipleFileField. It supports multiple files.
For example:
from wtforms import MultipleFileField
classNewFileForm(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)
Solution 2:
from wtforms import MultipleFileField
from werkzeug import secure_filename
classUpload_Form(FlaskForm):
files = MultipleFileField(render_kw={'multiple': True})
And the route
@app.route('/', methods=['GET', 'POST'])defupload():
form = Upload_Form()
if form.validate_on_submit():
for file in form.files.data:
file_name = secure_filename(file.filename)
file.save(file_name)
Post a Comment for "How To Upload Multiple Files With Flask-wtf?"