In ASP.NET MVC, FilePathResult is a class that is used to return a file to the client as a file download.
To use the FilePathResult, follow these steps:
- Create a controller method that returns an instance of the FilePathResult class.
csharppublic FilePathResult DownloadFile()
{
string fileName = "example.pdf";
string filePath = Server.MapPath("~/Content/" + fileName);
return new FilePathResult(filePath, "application/pdf");
}
In the controller method, specify the file path of the file that you want to download. In the example above, the file path is "~/Content/example.pdf".
Specify the content type of the file that you want to download. In the example above, the content type is "application/pdf".
In the view, create a link that calls the controller method that returns the FilePathResult object.
html<a href="@Url.Action("DownloadFile")">Download PDF</a>
When the link is clicked, the controller method will be called and the FilePathResult object will be returned, which will prompt the user to download the file.
Note: The file path should be a physical path to the file on the server. You can use the Server.MapPath method to convert a virtual path to a physical path.
Comments
Post a Comment