I have an input type="file"
tag in some views. When I submit the form, if there is a validation problem and my controller action returns the same/view model but with validation errors, the file selected for the file input is cleared. How can I preserve this selected file?

- 49,207
- 121
- 399
- 775
-
Is that not the default of ASP.Net FileUpload control as well? – codingbiz Sep 30 '12 at 14:12
2 Answers
You usually get the same behavior with input type="password"
tags. It is a security measure in browsers used to help protect users from malicious servers, who might try to gain access to a user's private sensitive files by pre-populating them in a file upload field.
If the file is invalid, why would you want to preserve it?
Reply to comment
If that is the case, you should be able to handle the other field validations on the client without doing a full postback. This would preserve the users' file upload entry since the page would not be re-rendered by the server after validation errors are discovered.

- 46,965
- 25
- 159
- 237
AFAIK this is not possible for security reasons. You cannot set the value
attribute of a file input field and this is the reason why you cannot populate it after postback.

- 1,023,142
- 271
- 3,287
- 2,928