2

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?

ProfK
  • 49,207
  • 121
  • 399
  • 775

2 Answers2

0

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.

danludwig
  • 46,965
  • 25
  • 159
  • 237
0

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928