IFrizy
IFrizy

Reputation: 1745

save input type file state after server validation mvc

I have html form with some textbox controls and <input type=file> control for uploading file. All controls have server validation. Imagine the next situation: I select file for uploading it and enter data for some textbox controls(some textboxes I leave blank). Then I click submit button and my form sended to the server action. After validation checking in server I redirect to the same form for error correction. All textboxes save state and have data but file control don't have.

P.S. When I debugging the code, I see uploaded file in view model. Can I resend it to html form? I don't write my code because I think it will be redundant.

Upvotes: 1

Views: 955

Answers (1)

Martin Tournoij
Martin Tournoij

Reputation: 27822

You can get the filename (eg. myfile.txt), but not the full path (eg. /home/martin/myfile.txt or C:/Users/martin/myfile.txt), so you can't set the value server-side or with Javascript. This is for reasons of security & privacy.

What you can do instead:

  1. Validate the form with Javascript or an AJAX request, if this fails, the original form (& values) are unchanged (I would recommend this).

  2. Save the file contents on the first try, and if the retry fails and the user didn't give a new file, use the saved file contents (this may require some special coding, and may leave orphaned files/db rows if the user didn't go for a second try & gave up instead. I wouldn't recommend this).

Upvotes: 1

Related Questions