[asp.net mvc]開啟pdf檔案於瀏覽器的另一個tab頁籤

  • 6400
  • 0

[asp.net mvc]開啟pdf檔案於瀏覽器的另一個tab頁籤

c#

[HttpGet]
public virtual ActionResult PdfOpen(string FileName)
{    

    //string FileName= "D:\\temp\\myPdf.pdf";

    if(checkLegalOk)
    {
        Response.AppendHeader("content-disposition", "inline; filename=file.pdf");//讓pdf直接於瀏覽器顯示而非下載
        return File(Server.MapPath("~/Content/pdf/" + FileName), "application/pdf");
    }
    else
    {
        return Content("未經授權的存取!");
    }

}


 cshtml:
ps. #view=FitH是要讓pdf可以自動縮放到最大以符合頁面大小

<td><a href="~/MyController/PdfOpen?FileName=myPdf.pdf#view=FitH" target="_blank" class="btn"><span>文的文件</span></a></td>


這樣就可以成功將pdf另外開啟在瀏覽器的另一個tab頁籤囉


這篇大概是這樣…

參考資料:
How to open PDF file in a new tab or window instead of downloading it using C# and ASP.NET MVC? - Stack Overflow
c# - How to return PDF to browser in MVC? - Stack Overflow