0

In my app I need to upload an image to the server, and user needs to preview the file before uploading it. So do it like this:

Show an image preview before upload

But after posted to server and there may be some errors, I have to return it back to client. Of course, the preview image loaded on client was lost, now I want to load it back again but I can't find the file to load using javascript. I have tried some ways:

$("file-input-selector").prop("files")

or:

document.getElementById("file-input-id").files

These only work when I select another image on the machine. After posted back from server both ways show 0 files

Is there another way to get the file after posted to the server back?

Hieu Dinh
  • 23
  • 5
  • You cannot set the value of a file input programatically for security reasons. The only way it can beset is by the user selecting a file. –  May 25 '17 at 12:26
  • @StephenMuecke I didn't try to set file input, I just tried to read the file after posted to the server and back. But I realized that the file is lost after that so there is no way I can read it from the input. Maybe I could try to store it in session or something but that's another matter. – Hieu Dinh May 29 '17 at 06:27
  • If the view is returned, you return the model and the `HtmlHelper` methods set the values of the inputs again. That cannot be done with a file input (it can only done by the user selecting a file in the browser). If you do not want the user to reselect the file, then you need to save the file and return the name of the file so they know its been saved. –  May 29 '17 at 10:19

0 Answers0