We will now move to the downloading section. This is the right time to add the remaining actions to our controller. The following is the code. DirectoryInfo Server. Add file. Now, the following code will explain what is happening here. Next, here's the need to set another view. The following code is for the Download View. ActionLinks dynamically.
Please note that we are adding the Image name to the action. Here is the output after performing the operations. As in the preceding image, when you mouse over the link, it will show the image name along with the controller URL. Click on the link to download the file. So simple, right?
I hope you liked the article. Please provide your valuable feedback; it matters a lot. You can download the source code to determine more. View All. Sibeesh Venu Updated date Jan 25, I hope you all are fine. Please see this article in my blog here. 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. 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. In the File method line 7 , the second parameter contentType is used in the HTTP header to indicate the format of contents transmitted over the internet. 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.
In the responses section, the Swagger UI provides a link for downloading the returned file. Sometimes, the file type should be determined at runtime. Then in this case, we should set the contentType based on the file extension. NET Core natively supports this kind of translation. An example implementation is as follows. 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.
In this script we are reading the url from the link and call the controller method using Ajax. When you add a new js file to your project, you have to register the file in the bundle config file. Please refer to this article to learn how to add custom scripts to a project. Then the scripts inside the custom js file will be loaded automatically into browser. Make sure to register it after the jquery registration.
Otherwise the jquery will give you some errors. Now the methods and calling parts are done and we have to implement the data reading and file generating part. For the data reading part I just used a hard coded data and you can read the data from the database. Finally we have the working application…. To check the solution just click on the link and it will download csv file which contains comma separated values as we defined. Your email address will not be published. Notify me of follow-up comments by email.
Notify me of new posts by email. This site uses Akismet to reduce spam. Learn how your comment data is processed. NET MVC Sometimes we have to create csv comma separated data files from our web applications and let the end user to download csv files.
0コメント