1

I have a problem with an form on my website. The form contains some text inputs and a file upload input. When some text fields aren't filled correctly, there come's an error and the page is reloaded. But: If I choose a file, don't fill out the other files correctly and the page is reloaded, I must choose my file again, it isn't saved in the form.

Can anybody help me please?

user229044
  • 232,980
  • 40
  • 330
  • 338
user3399048
  • 11
  • 1
  • 2
  • You should just perform validation client-side before submitting the form (in addition to your server-side validation which is mandatory) to minimize the chances the user will submit invalid data and have to repopulate the form. – user229044 Mar 09 '14 at 17:39

1 Answers1

3

For security reasons, you cannot set the value of a file input at either client or server.

I can think of these possible work-arounds:

  1. This post provides one method of a server-side solution involving putting an ID in the page that refers to the previously uploaded files rather than the actual file inputs themselves (since the files are likely already uploaded).

  2. Many sites that accept file uploads do them in a somewhat separate form. The file uploads are completed and then the rest of the form is filled out and submitted with the server keeping a reference connection between the previously uploaded files and the current action that will be submitted to the server. The popular forum software vBulletin works that way for message attachments. Attachments are uploaded in a separate form/UI.

  3. You can submit your form via ajax, rather than as a form submission. Because the ajax call does not change the current page, if you get errors from the submission via ajax, then the current page and the selected file inputs are still there. You can add your error reports to the current page with javascript and show the users the error that way.

  4. You can also avoid most of this issue (thought not all), by doing as much client-side validation as possible with javascript before submitting your form so you lessen the possibilities of the server ever rejecting the form submission.

jfriend00
  • 683,504
  • 96
  • 985
  • 979