2

I have a ViewModel:

    public HttpPostedFileBase File { get; set; }
    public string Notes { get; set; }

In the Create view, I have an <input type="file" name="file" /> that binds the HttpPostedFile to the model. So far, so good.

My problem begins when I try to develop the "Edit" action with their respective view. I haven't problems at all to show the editor field for the string property Notes, that part is easy.

However, how do I show the <input type="file" name="file" /> with the actual value in the Edit view? How do I to bind a byte array to the File property of the ViewModel? Is that the correct way? Is there another better solution? I'm stranded with this and would appreciate your help.

kush
  • 979
  • 1
  • 15
  • 32
  • As a file input type is just for *uploading* and not editing a file in the browser what do you want to achieve? – Marc Nov 23 '12 at 04:32

1 Answers1

0

You can't. An <input type="file" /> is not made for editing files, just for uploading them.

If you just want to be able to delete the file in the edit view try this (just an example):

  1. Add a bool property DeleteFile to the edit view model
  2. Add that property as a checkbox in the edit view
  3. In your edit method check if DeleteFile is set to true and if so, find and delete the file attached to that record you are editing

And you could rename File to UploadedFile (so its a bit clearer). But thats up to you of course.

Marc
  • 6,749
  • 9
  • 47
  • 78
  • Thanks for your answer Marc. But in the Edit view, the user must change the file, not delete it (the File is required). I think I'll have to put a bit of Jquery to control that... Thanks again! – Fernando Verdaguer Nov 25 '12 at 17:08
  • You can show the user the file upload control so he can upload a new file and you replace the old file on the server side. And you can show the user that a file exists on the server but there is no way you can set the value of a file upload control (as this would be a security leak). – Marc Nov 25 '12 at 23:50
  • That's the point. I'll have to do that, show the actual filename (for example) and check in the controller if the File property of the model is null or not (to upload the new one and update the DB). Thanks, best regards! – Fernando Verdaguer Nov 26 '12 at 14:20