Open PDF result in browser tab with MVC 3

前端 未结 3 996
有刺的猬
有刺的猬 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:52

    Try linking directly to the PDF.... If it opens in a tab then try changing your route to end with .pdf

    0 讨论(0)
  • 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")
    
    0 讨论(0)
  • 2020-12-31 07:13

    This is configurable in your browser. You can change the settings to download / open in browser or open in relevant application in tools->options->applications section for all file types. Thi has nothing to do with your code.

    0 讨论(0)
提交回复
热议问题