Open PDF result in browser tab with MVC 3

前端 未结 3 1015
有刺的猬
有刺的猬 2020-12-31 06:22

I am using ASP.NET MVC 3. I have a controller action that returns a PDF file like this:

Public Class ReportController
    ...
    Function Generate(id As Int         


        
3条回答
  •  太阳男子
    2020-12-31 06:55

    I got the answer from the related links on the right:

    Response.AppendHeader("Content-Disposition", "inline")
    Return File(output, "application/pdf")
    

    The PDF opens in a tab, but the filename hint is lost, even if I do it like this:

    Response.AppendHeader("Content-Disposition", "inline; filename=something.pdf")
    Return File(output, "application/pdf", "something.pdf")
    

    So finally I didn't bother to give filename hint at all.

    EDIT

    ASP.NET MVC 3's File with 3 parameters:

    Return File(output, "application/pdf", "something.pdf")
    

    will add Content-Disposition: attachment; filename="something.pdf" to the response header, even if there is already a Content-Disposition in the response header.

    So if you manually added Content-Disposition to the header, and then use File with 3 parameters, you end up with two Content-Disposition headers. Firefox 8 will say that the response is corrupted if the response header is like this.

    So best way to do it now is add Content-Disposition manually for 'inline', and then use File with 2 parameters:

    Response.AppendHeader("Content-Disposition", "inline; filename=something.pdf")
    Return File(output, "application/pdf")
    

提交回复
热议问题