Flask-WTF File contents lost when form fails validation and user resubmits form

152 views Asked by At

I'm having an issue where the contents of an uploaded file, via a FileField, are lost when the user resubmits form. I'm guessing the easy answer is to force the user to re-upload the file however I was wondering if there might be a workaround that can avoid having the user re-upload.

1

There are 1 answers

1
junnytony On BEST ANSWER

As @dirn stated, that is the nature of file uploads. You have two options to get around this.

  1. Save the uploaded file temporarily (especially if its large) while you prompt the user to fix the input error (as suggested by @dirn). This would require extra logic to purge files (assuming the user decides they don't want to submit form anymore or they go to a different page, etc)
  2. Validate your form using javascript so that the file only uploads when the form is actually valid (wtforms doesn't really help you much with this option)