Reputation: 25
I'm trying to save multiple files through WTForm. Since there is no documentation on how you need to use MultipleFileField I looked on SO and came accross multiple answers. This one looked promising but it doesn't work. The file
variable is a string and thus the code doesn't work.
class CreatePostForm(FlaskForm):
files = MultipleFileField('Upload files', validators={DataRequired()})
submit = SubmitField(_l('Submit'))
@app.route('/create_post', methods=['GET', 'POST'])
@login_required
def create_post():
form = CreatePostForm()
if form.validate_on_submit():
files_filenames = []
for file in form.files.data:
file_filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file_filename))
files_filenames.append(file_filename)
return redirect(url_for('index'))
return render_template('create_post.html', form=form)
Upvotes: 0
Views: 824
Reputation: 25
Turns out the MultipleFileField doesn't add the enctype="multipart/form-data" attribute to the html form. You have to do this manually to make this work.
So the above code works with this html page.
<form action="" method="post" class="form" role="form" enctype="multipart/form-data">
{{ wtf.quick_form(form) }}
</form>
Upvotes: 0