Flask File Uploads Passing data to WTForms and Flask-WTF


Example

WTForms provides a FileField to render a file type input. It doesn't do anything special with the uploaded data. However, since Flask splits the form data (request.form) and the file data (request.files), you need to make sure to pass the correct data when creating the form. You can use a CombinedMultiDict to combine the two into a single structure that WTForms understands.

form = ProfileForm(CombinedMultiDict((request.files, request.form)))

If you're using Flask-WTF, an extension to integrate Flask and WTForms, passing the correct data will be handled for you automatically.

Due to a bug in WTForms, only one file will be present for each field, even if multiple were uploaded. See this issue for more details. It will be fixed in 3.0.