FilePathResult in .NET MVC with code examples

When developing web applications, handling file downloads is a common requirement. In .NET MVC, the FilePathResult class provides a convenient way to serve files for download. It allows you to return a file from the server to the client without exposing the actual file path. In this blog post, we will explore the usage of FilePathResult and demonstrate how it can be implemented with a few code examples.

What is FilePathResult?

FilePathResult is an ActionResult type in the ASP.NET MVC framework that represents a file result to be sent to the client. It encapsulates the file path and the content type of the file. The framework takes care of sending the file to the client as a download, rather than displaying it in the browser.

How to Use FilePathResult?

To use FilePathResult, you need to follow these steps:

  1. Create an action method in your controller that returns a FilePathResult object.
  2. Specify the file path and the content type of the file.
  3. Return the FilePathResult object from the action method.

Let's dive into some code examples to see how it works.

Example 1: Downloading a PDF File

In this example, we will create an action method that allows the user to download a PDF file named "sample.pdf".

public ActionResult DownloadPDF()
{
    string filePath = Server.MapPath("~/Files/sample.pdf");
    string contentType = "application/pdf";

    return File(filePath, contentType, "sample.pdf");
}

In the above code snippet:

  • Server.MapPath is used to get the physical file path from the virtual path.
  • The File method is used to create a FilePathResult object, specifying the file path, content type, and file name.

Example 2: Serving a CSV File

Suppose we have a requirement to serve a CSV file named "data.csv". Here's an example of how it can be achieved:

public ActionResult DownloadCSV()
{
    string filePath = Server.MapPath("~/Files/data.csv");
    string contentType = "text/csv";

    return File(filePath, contentType, "data.csv");
}

The code is similar to the previous example, but the content type is set to "text/csv" to indicate that it is a CSV file.

Example 3: Downloading a ZIP Archive

Let's say we want to provide a download link for a ZIP archive containing multiple files. Here's an example:

public ActionResult DownloadZip()
{
    string filePath = Server.MapPath("~/Files/archive.zip");
    string contentType = "application/zip";

    return File(filePath, contentType, "archive.zip");
}

In this case, the content type is set to "application/zip" to indicate that it is a ZIP file.

Example 4: Serving Images

FilePathResult can also be used to serve image files. Here's an example that allows users to download an image file named "image.jpg":

public ActionResult DownloadImage()
{
    string filePath = Server.MapPath("~/Files/image.jpg");
    string contentType = "image/jpeg";

    return File(filePath, contentType, "image.jpg");
}

The content type is set to "image/jpeg" to indicate that it is a JPEG image file.

Example 5: Controlling the File Download

By default, when a user clicks on a download link, the file will be downloaded. However, you can control the behavior using additional parameters. For example, you can set the fileDownloadName parameter to change the downloaded file's name:

public ActionResult DownloadFile()
{
    string filePath = Server.MapPath("~/Files/sample.docx");
    string contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";

    return File(filePath, contentType, "document.docx", fileDownloadName: "my_document.docx");
}

In this example, the downloaded file will be named "my_document.docx" instead of "document.docx".

The FilePathResult class in .NET MVC simplifies the process of serving files for download. By using this class, you can provide users with a seamless file download experience without exposing the actual file path on the server. This blog post demonstrated the usage of FilePathResult with various code examples, showcasing its versatility in handling different file types. With this knowledge, you can now enhance your web applications by implementing secure and efficient file downloads.

 
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
4 + 4 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice