ASP.NET MVC 項目直接預覽PDF檔案

來源:互聯網
上載者:User

標籤:att   text   header   ext   代碼   return   head   檔案   ice   

背景及需求

 

  項目使用的是MVC4架構,其中有一個功能是根據設定產生PDF檔案,並在點擊時直接預覽。

 

實現過程

1、第一版實現代碼:

  HTML內容

@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Index</title></head><body>    <div>         @Html.ActionLink("預覽PDF","GetPdf",null,new { target="_blank"})    </div></body></html>

  控制器代碼

        public ActionResult GetPdf()        {            return new FilePathResult("~/content/The Garbage Collection Handbook.pdf", "application/pdf");        }

  缺點:標題和檔案下載時名稱不是很友好。

 

1、第二版實現代碼:

  我們做了2件事情:

    1、讓下載彈出框能顯示友好的下載檔案名稱。

    2、讓瀏覽器中的其他兩個顯示GetPdf的地方也顯示友好的內容。

  自訂ActionFilter,對Header進行修改,變為內聯。(直接這麼替換不知道會不會有隱患。)

 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);            }        }    }

  自訂ActionNameSelector實現對Action名稱的攔截和判斷。

    public class MyActionNameSelecter : ActionNameSelectorAttribute    {        public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)        {            return actionName.Contains("-PDF檔案預覽");        }    }

  控制器內代碼修改如下

        [MyActionNameSelecter]        [MyPdfActionFilter]        public ActionResult GetPdf()        {            return new FilePathResult("~/content/The Garbage Collection Handbook.pdf", "application/pdf")            //增加FileDownloadName設定,但是這會讓內容以附件的形式響應到瀏覽器(具體參考檔案響應模式:內聯和附件)。            //檔案變成被瀏覽器下載。            { FileDownloadName = "The Garbage Collection Handbook.pdf" };        }

  頁面內容修改如下

@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Index</title></head><body>    <div>         @* 第二個參數可能是一個動態產生的內容,需要ACTION中增加名稱選擇攔截,所以自訂了一個ActionNameSelectorAttribute類滿足要求。 *@        @Html.ActionLink("預覽PDF", "The Garbage Collection Handbook-PDF檔案預覽", null,new { target="_blank"})    </div></body></html>

 

最終效果

 

 

ASP.NET MVC 項目直接預覽PDF檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.