FileContentResult example in .NET MVC

If you're working with files in .NET MVC, you may need to return a file from your controller action method to the client. This is where FileContentResult comes in handy. In this blog post, we'll explore what FileContentResult is and how it can be used in your .NET MVC application.

What is FileContentResult?

FileContentResult is a class in the .NET Framework that returns a file to the client as an action result. It returns the contents of a file as an array of bytes, along with a specified MIME type that indicates the type of file being returned.

How to use FileContentResult?

Here are some examples of how you can use FileContentResult in your .NET MVC application:


Example 1: Downloading a file

Suppose you have a file called "sample.pdf" that you want to allow users to download. Here's how you can do it using FileContentResult:

public ActionResult DownloadFile()
{
    byte[] fileContents = System.IO.File.ReadAllBytes(@"C:\Path\To\sample.pdf");
    return File(fileContents, "application/pdf", "sample.pdf");
}

Example 2: Streaming a file

If you're dealing with large files, you may not want to read the entire file into memory before returning it to the client. Instead, you can stream the file to the client using FileContentResult:

public ActionResult StreamFile()
{
    var stream = new System.IO.FileStream(@"C:\Path\To\largefile.pdf", System.IO.FileMode.Open);
    return File(stream, "application/pdf");
}

Example 3: Returning a file as a byte array

If you have a byte array that represents the contents of a file, you can use FileContentResult to return it to the client:

public ActionResult GetFile()
{
    byte[] fileContents = SomeMethodThatReturnsFileContents();
    return File(fileContents, "application/octet-stream", "filename.ext");
}

Example 4: Customizing file download behavior

You can customize how the file is downloaded by using the FileDownloadName property of FileContentResult. For example, you can force the file to be downloaded instead of opened in the browser:

public ActionResult DownloadFile()
{
    byte[] fileContents = System.IO.File.ReadAllBytes(@"C:\Path\To\sample.pdf");
    var result = File(fileContents, "application/pdf");
    result.FileDownloadName = "sample.pdf";
    return result;
}

Example 5: Setting cache control headers

You can also set cache control headers for the file being returned using the HttpCacheability property of FileContentResult. For example, you can set the cache control header to prevent caching:

public ActionResult DownloadFile()
{
    byte[] fileContents = System.IO.File.ReadAllBytes(@"C:\Path\To\sample.pdf");
    var result = File(fileContents, "application/pdf");
    result.FileDownloadName = "sample.pdf";
    result.HttpCacheability = HttpCacheability.NoCache;
    return result;
}

In this blog post, we've explored what FileContentResult is and how it can be used to return files to the client in your .NET MVC application. Whether you're downloading a file, streaming a large file, or returning a file as a byte array, FileContentResult is a versatile tool that can help you get the job done.

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