Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got a simple form in an MVC app I've made. It contains a file field so users can upload an image. It all works great.

Problem is, if the form submit fails validation the content of the file field is lost (other fields remain populated, thx HtmlHelpers!). How do I keep the file field populated after a failed validation?

TIA!

like image 663
Chaddeus Avatar asked Jun 09 '09 02:06

Chaddeus


2 Answers

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.

like image 147
Alexander Prokofyev Avatar answered Sep 22 '22 04:09

Alexander Prokofyev


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.

like image 45
Michael Avatar answered Sep 23 '22 04:09

Michael