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