How to return a file from an API endpoint in ASP.NET Core and create an HTML download link.
- If you want to download a large file or a full folder then first zip your file or folder in order to compress it and then download it. Step 1: Login to the server using the SSH login details. You can use softwares are putty or Terminal to access your server from SSH.
- Using REST Services to upload and download files. 11 May 2018 by F.Marchioni. This rest service tutorial i s a quick guide for handling files upload and download using REST Services. We will demonstrate with a rest service sample how to upload and download files using JAX-RS API with different front ends (AngularJS and JSP).
Select a download folder if prompted. Some browsers, such as Internet Explorer, will ask you to click a folder (e.g., your desktop) in which to save your file. Chrome, Firefox, and Safari will all begin downloading the file immediately by default. On Safari, you can click the downward-facing arrow in the upper-right side of the browser window.
In this article, I will use a demo Web API application in ASP.NET Core to show you how to transmit files through an API endpoint. In the final HTML page, end users can left-click a hyperlink to download the file or right-click the link to choose “
Save Link As” in the context menu and save the file.
The full solution can be found in my GitHub repository, which includes a web project for uploading/downloading files and an integration test project for testing controller methods.
Now let’s dive in.
In ASP.NET Core, a Web API action method usually returns an
ActionResult object. When we want to return a file response, we can explicitly set the return type for the action method to be
FileResult, which is a type inherited from
ActionResult. But most times we still want to use the generic
ActionResult because it covers many other useful return types, such as
NoContent, and so on.
FileResult type is an abstract type, and it has four concrete implementations:
PhysicalFileResult. These four variants don’t differ much, you can use any of them to construct a
FileResult object at your convenience. In other words, no matter which of the four types you use, the client-side will not notice any difference when downloading a file from the API endpoint.
Apart from the constructors for the four concrete types, we can use a method
File, provided natively from the
ControllerBase class, to return a concrete
FileResult object. The
File method is very flexible and has a variety of overload methods to instantiate
With the groundwork laid, we can easily write an action method as follows:
In the code above, we first find or generate the requested file from the local file system or a file storage, then read the file content. In the end (line 7), we return a
FileContentResult object that is created using the method
File(byte fileContents, string contentType, string fileDownloadName). Note that we should validate the request and user permissions before finding or generating the requested file. And you can choose to stream the file too.
File method (line 7), the second parameter
contentType is used in the HTTP header to indicate the format of contents transmitted over the internet. In this demo, the action method returns a text file, so the content type is “
text/plain”. You can set the value for
contentType according to your file type. In the
File method, the third parameter
fileDownloadName determines the value for the
filename attribute in the
content-disposition HTTP response header. Browsers treat this value as the top priority to dictate the filename when downloading the file.
To sum up, the
File method in line 7 sets the HTTP response headers as follows.
If the project includes Swagger support, then we can try out the API endpoint in the Swagger UI page. In the responses section, the Swagger UI provides a link for downloading the returned file.
Sometimes, the file type should be determined at runtime. For example, some attachments are PDF files, some files are in CSV format, some are images, and so on. Then in this case, we should set the
contentType based on the file extension. ASP.NET Core natively supports this kind of translation. An example implementation is as follows.
In lines 8 to 12, we utilize a
FileExtensionContentTypeProvider to get
contentType based on the file path. The
FileExtensionContentTypeProvider is provided in the NuGet package
Microsoft.AspNetCore.StaticFiles (refer to the
using statement in line 1). This NuGet package is automatically included in ASP.NET Core Web projects, and it provides mappings between many commonly seen file extensions and content types. If the file extension is not in the mappings table, then you can add desired mappings in the following way.
If this utility is being used in many places, then you can extract it into a service class that focuses on content-type mapping.
How Long To Download File Calculator
href attribute has a value that points to the API endpoint. Here the
href value is
api/students/files/1, which can be dynamically set in the front-end code according to the path and parameters of our API endpoint. Notice that the URL doesn’t have to include the filename or file extension.
Another attribute in the anchor element is the
download attribute, which prompts the user to save the linked URL instead of navigating to it. You can read more about the
download attribute here.
How Long To Download File
The rendered hyperlink works in all major browsers like Chrome, Edge, and Firefox. Users can either left-click a download link to download the file or right-click the link to choose “
Save Link As” in the context menu and save the file. It works like the screen recording below.
Everything works as expected. Awesome!
That’s all for today. We have gone over the details about downloading files via Web API. I hope you have learned something new.
How Long To Download Zip Files
Thanks for reading.