Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Input type file not getting populated in Edit View ASP.NET MVC

I have this model:

public class PostAddRequest
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string FeaturedImagePath { get; set; }
        public HttpPostedFileBase FeaturedImage { get; set; }
    }

Now, in the controller I cannot find how to populate the HttpPostedFileBase property, so that the input type field would be populated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This can’t be done, because it shouldn’t be done. Change your approach.

You don’t want to pre-populate a file input from the server. Even if it were possible, consider what would be involved in a scenario where the user submits the form without changing the file:

  • The server would send the entire file to the client
  • The client would save the file to the file system
  • The client would re-send the entire file back to the server
  • The file would still be saved somewhere on the client for no reason? What then?

Take a step back and re-think the approach. The file input is initially empty and there for the purpose of the user selecting a new file if they want to.

To show the user that a file already exists on the server, provide a UI separate from the file input. It could be something as simple as this:

<a href="/files/123">Filename.ext</a> (click to download)
<a href="/files/123/delete">Delete?</a>
Select a new file: <input type="file" name="file" />

Or anything along those lines of functionality. Basically you have three operations available:

  • Get the file
  • Delete the file
  • Upload a new file

Those are separate operations, invoked by separate UI elements. The file input only covers one of those operations.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading