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.
CodePudding user response:
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.