Ameba Ownd

アプリで簡単、無料ホームページ作成

Mvc 5 pdf download

2021.12.17 21:55






















Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. In WebForms, I would normally have code like this to let the browser present a "Download File" popup with an arbitrary file type, like a PDF, and a filename:. Return a FileResult or FileStreamResult from your action, depending on whether the file exists or you create it on the fly.


If you want to let the browser handle by its default behavior plugin or download , just send two parameters. You should look at the File method of the Controller. This is exactly what it's for. Although standard action results FileContentResult or FileStreamResult may be used for downloading files, for reusability, creating a custom action result might be the best solution.


As an example let's create a custom action result for exporting data to Excel files on the fly for download. Since we are downloading the file using HttpGet, create an empty View without model and empty layout.


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. How can I present a file for download from an MVC controller? Ask Question.


Asked 8 years, 5 months ago. Active 8 years, 5 months ago. Viewed 11k times. Tim Hobbs 1, 16 16 silver badges 23 23 bronze badges. Michael Weston Michael Weston 1 1 1 gold badge 1 1 silver badge 2 2 bronze badges.


What does your browser's F12 tools say about network activity and headers sent? Do you want the browser to save the file use content-disposition or view it in the window? I added the content disposition in earlier Add a comment. Active Oldest Votes. It also explains the different results that build on FileResult : If the content you want to transfer is stored within a disk file, you can use the FilePathResult object.


Create Procedure [dbo]. After clicking Add button, it will show in the Window. Specify the Controller name as Home with suffix Controller. Now, let's modify the default code of Home controller. After modifying the code of Homecontroller class, the code will look like-. Models; using System; using System.


Generic; using System. IO; using System. Linq; using System. Web; using System. Mvc; using Dapper; using System. Configuration; using System. SqlClient; using System. This method searches an Employee based on its id and returns a view object showing an Employee information. The Action method PrintSalarySlip accepts an id parameter. The constructor of this class accepts IndexById view. Step 6: To add view for the action methods, right click inside the Index action method and select option Add View.


Select List view template and EmployeeInfo model class. This will add Index. Replace the Create New action link as shown in the following code:. Remove Edit, Details and Delete action link from the bottom of the Index. Doing so will add IndexById. This view will be used to show the Salary Slip.


The view will have the following code:. Step 8: In the RouteConfig. Click on the Print Salary Slip link for any record, the PDF result of the Salary Slip for the selected record will be display as shown in the following image:. Download the entire source code of this article Github. This article has been editorially reviewed by Suprotim Agarwal. C and. Organized around concepts, this Book aims to provide a concise, yet solid foundation in C and.