How to keep input type=file field value after failed validation in ASP.NET MVC?

Browsers are designed in such way because of security risks. It's impossible to set value of file input box in HTML source or by Javascript. Otherwise malicious script could steal some private file without user attention.

There is an interesting information about the subject.


As far as I know you cannot set the value of a HTML file input box. I would suggest coupling the file input box with a label or text box.

Then you can populate the it with the value from the file input box to be resubmitted later.


If the file isn't too big you could base64 it and use this as value for a hidden field.


There are flash based file uploaders. Try one of them. Some of them even falls back to regular file input box if flash and java script is not supported. I advise to look for jQuery plugins.


I would recommend doing the validation beforehand via ajax and do a partial page update. In this case, you will not lose the file.