Return or Download File in ASP.NET Core API

Download File in ASP.NET Core API

In this article, we will see how to download or return useful file types like .pdf, .txt, .xlsx, etc in the browser or any other type of application. We will use ASP.NET Core WebAPI as a sample to verify how to download Files in ASP.NET Core-based API or MVC application.

Most of the time, we get requirements to download or return the file within the given ASP.NET Core or Angular or any other UI applications dealing with it.

We shall see how to perform a download or file return in ASP.NET core-based application. This file download or return technique can be used for any ASP.NET Core, Angular app.

The file helper method is built into your controller. These methods are coming from the FileResult abstract class and gives you multiple options for retrieving files to send to the client.

1. Using FileStreamResult for File Download

FileStreamResult classes us useful for Sending binary content.

Here you have a stream and want to return stream content as a file.

Example:

Download or Return Image (.png) file in .NET

Here we will be returning an image from the Controller method. Using the below technique controller can return any other types of images like .bmp or .jpg etc.

For png file we need to use mimeType = "image/png.

                  [HttpGet("{id}")]         [Route("Stream")]         public async Task<IActionResult> DownloadImage(string id)         {             Stream stream  = _fileInterafces.ReadAsStream(id);             string mimeType = "image/png";             return new FileStreamResult(stream, mimeType)             {                 FileDownloadName = "image.png"             };         }                

Note: _fileInterafces is my custom interface that reads the file and returns streams .

Download or Return PDF(.pdf) file in .NET

Here we will be returning a PDF file from the Controller method. Using the below technique controller can return any other types of file as well like .doc or .txt etc.

For PDF file we need to use mimeType = "application/pdf.

                  [HttpGet("{id}")]         [Route("Stream")]         public async Task<IActionResult> DownloadPdfFile(string id)         {                         Stream stream = _fileInterafces.ReadAsStream(id);             string mimeType = "application/pdf";             return new FileStreamResult(stream, mimeType)             {                 FileDownloadName = "FileasStream.pdf"             };         }                

In about code _fileInterafces can be the provider of your choice or This could be your custom code dealing with the files.

Download or Return Excel(.xlsx) file

Here we will be returning or downloading an Excel file(.xslx) from the Controller method. Using the below technique Controller can return any other types of images like .xlx or .csv etc.

For excel file we need to use mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

                  [HttpGet("{id}")]         [Route("Stream")]         public async Task<IActionResult> DownloadXlsxFile(string id)         {             Stream stream = _fileInterafces.ReadAsStream(id);             string mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";             return new FileStreamResult(stream, mimeType)             {                 FileDownloadName = "FileasStream.xlsx"             };         }                

2. Using FileContentResult for File Download

Here we will be returning an Excel file(.xslx) from the Controller method.

Here we will be returning a stream of bytes to an excel file.

Example

                  [HttpGet("{id}")]         [Route("Stream")]         public async Task<IActionResult> DownloadfromBytes(string id)         {             byte[] byteArr = _fileInterafces.ReadAsByteStream(id);             string mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";             return new FileContentResult(byteArr, mimeType)             {                 FileDownloadName = "excelfromByte1.xlsx"             };         }                

3. Using File

You cal also use the File class for file processing. Please see the below example to achieve the same.

return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "FileasStream.xlsx");

Difference between FileContentResult FileStreamResult

As we know FileResult is an abstract base class. Both FileContentResult & FileStreamResult classes are derived class from Abstract FileResult class.

FileStreamResult – Sends binary content to the response by using a Stream instance. Here you have a stream and want to return stream content as a file.

FileContentResult – Sends the contents of a binary file to the response. Here you have a byte array and want to return byte content as a file.

MIME Types for files upload and download

Below are a few examples of MIME – content types for your information.

  • Text File – "text/plain"
  • PNG image – "image/png"
  • JPEG image – "image/jpeg"
  • MSDoc – "application/vnd.ms-word"
  • MSDoc – "application/vnd.ms-word"
  • PDF file – "application/pdf"
  • Excel File- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

One can also use the below mime with the content type as "application/octet-stream" which is a binary file and kind of generic in nature and works for most types of files.

To view, one should use an appropriate extension before downloading a file.

mimeType = "application/octet-stream";

References:

  • Read a Large File in Chunks in C# – Guidelines
  • Using IFormFile for file upload

Hope this helps.

Please sound off your comments below!



Please bookmark this page and share this article with your friends and Subscribe to the blog to get a notification on freshly published best practices of software development.