Return HttpPostedFileBase to view validation error

When I try to load images into the action of my MVC controller and a validation error occurs, I have to click each of the buttons and again find all my files.

If I have a view consisting of

<input type="file" id="file0" name="Files[0]" /> <input type="file" id="file1" name="Files[1]" /> 

and controller actions, for example

 public ActionResult Create(ModelClass model, IEnumerable<HttpPostedFileBase> Files) { if(ModelState.IsValid) { //do work if(PhotoValidation.IsValid(Files)) { //do work } else { ModelState.AddModelError("","Photos not valid"); } } return view(model); // Way to return photos back to the view on modelstate error? } 

Files can be sent to the server in order, but if there is an error checking the model, is there a way to return the model AND the files so that the user does not upload them again?

+6
source share
1 answer

It's impossible. The value intput type = "file" is never used when parsing an HTML page. This is a huge security risk, so no modern browser will allow them to "save" the values.

If you are sure that your clients are using an HTML5-enabled browser, you can try using the JavaScript file APIs to eliminate postbacks - check the following articles:

+8
source

Source: https://habr.com/ru/post/925356/


All Articles