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.
Here we will be returning an image from the Controller method. Using the below technique controller can return any other types of images like. 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.
Here we will be returning or downloading an Excel file. Using the below technique Controller can return any other types of images like. StaticFiles refer to the using statement in line 1. 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.
Web users have become used to clicking a hyperlink to download a file. There are many ways to implement the hyperlink. For example, we can create a blob data URL, use a third-party JavaScript library, or serve files directly from a web server. In this article, we are going to use the simplest way to create an anchor element without any JavaScript.
The code is just one line, as shown in the following snippet. The href attribute has a value that points to the API endpoint. 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. The rendered hyperlink works in all major browsers like Chrome, Edge, and Firefox. It works like the screen recording below. Everything works as expected. We have gone over the details about downloading files via Web API.
I hope you have learned something new. More importantly before I end up doing the job one way or the otther, I wanted to know what's the "right" way of doing this sort of job Right, for your above scenario the action does not need to return an async action result. You can check my comments in the below code here. Methods are asynchronous if return a Task object, not because are decorated with async keyword. This lowers the chances of running out of thread pool threads. So the answer to your question "Does the above needs to be async at all?
I am only looking to return stream. Is that okay? How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 7 years, 9 months ago. Active 6 years, 10 months ago.
0コメント