ASP. NET MVC settings generate PDF files, and you can click Preview

Source: Internet
Author: User
Tags httpcontext actionlink


This article mainly introduced the ASP. NET MVC Project Realization Direct Preview PDF file method, has the very good reference value, follows with the small compilation together to look down









Background and requirements






The project uses the MVC4 framework, which has a feature to generate PDF files based on the settings and preview them directly when clicked.






Implementation process






1, the first version of the implementation code:






HTML content


@ {

Layout = null;

}


<! DOCTYPE html>

<html>

<head>

<meta name = "viewport" content = "width = device-width" />

<title> Index </ title>

</ head>

<body>

<p>

@ Html.ActionLink ("Preview PDF", "GetPdf", null, new {target = "_ blank"})

</ p>

</ body>

</ html>

Controller code


public ActionResult GetPdf ()

{

 return new FilePathResult ("~ / content / The Garbage Collection Handbook.pdf", "application / pdf");

}

Cons: Titles and names are not very friendly when downloading.


1. The second version of the implementation code:


We did 2 things:


1. Make the download pop-up box display friendly download file names.


2. Make the other two places in the browser where GetPdf is displayed also display friendly content.


Customize the ActionFilter and modify the Header to become inline. (I do n’t know if there will be hidden dangers.)


public class MyPdfActionFilter: ActionFilterAttribute

{

public override void OnResultExecuted (ResultExecutedContext filterContext)

{

 //Content-Disposition=attachment%3b+filename%3d%22The+Garbage+Collection+Handbook.pdf%22}

 var filerHeader = filterContext.HttpContext.Response.Headers.Get ("Content-Disposition");

 if (! string.IsNullOrEmpty (filerHeader) && filerHeader.Substring (0, "attachment" .Length) .ToLower (). Equals ("attachment"))

 {filterContext.HttpContext.Response.Headers ["Content-Disposition"] = "inline" + filerHeader.Substring ("attachment" .Length, filerHeader.Length-"attachment" .Length);

 }

}

}

Custom ActionNameSelector implements interception and judgment of Action name.


public class MyActionNameSelecter: ActionNameSelectorAttribute

{

public override bool IsValidName (ControllerContext controllerContext, string actionName, MethodInfo methodInfo)

{

 return actionName.Contains ("-PDF file preview");

}

}

The code in the controller is modified as follows


[MyActionNameSelecter]

[MyPdfActionFilter]

public ActionResult GetPdf ()

{

 return new FilePathResult ("~ / content / The Garbage Collection Handbook.pdf", "application / pdf")

 // Increase the FileDownloadName setting, but this will make the content respond to the browser as an attachment (for specific reference file response mode: inline and attachment)

 // The file becomes downloaded by the browser.

 {FileDownloadName = "The Garbage Collection Handbook.pdf"};

}

The page content is modified as follows


@ {

Layout = null;

}

<! DOCTYPE html>

<html>

<head>

<meta name = "viewport" content = "width = device-width" />

<title> Index </ title>

</ head>

<body>

<p>

@ * The second parameter may be a dynamically generated content that requires name selection interception to be added to ACTION, so a custom ActionNameSelectorAttribute class is customized to meet the requirements. * @

@ Html.ActionLink ("Preview PDF", "The Garbage Collection Handbook-PDF file preview", null, new {target = "_ blank"})

</ p>

</ body>

</ html>

final effect


【related suggestion】


1. ASP.NET Free Video Tutorial


2. ASP.NET Tutorial


3. Geek Academy ASP.NET Video Tutorial



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.