Generating PDF, error with IE and HTTPS

前端 未结 12 511
醉酒成梦
醉酒成梦 2020-12-08 09:17

I am streaming a PDF to the browser in ASP.NET 2.0. This works in all browsers over HTTP and all browsers except IE over HTTPS. As far as I know, this used to work (o

相关标签:
12条回答
  • 2020-12-08 09:32

    We have faced a similar problem long time back - what we did was we (this is Java EE). In the web application config we add

    <mime-mapping>
        <extension>PDF</extension>
        <mime-type>application/octet-stream</mime-type>
    </mime-mapping>
    

    This will make any pdf coming from your web application to be downloaded instead of the browser trying to render.

    EDIT: looks like you are streaming it. In that case you will use a mime-type as application/octet-stream in your code and not in the config. So here instead of

    Response.ContentType = "application/pdf"
    

    you will use

    Response.ContentType = "application/octet-stream"
    
    0 讨论(0)
  • 2020-12-08 09:32

    try to disable gzip compression.

    0 讨论(0)
  • 2020-12-08 09:33

    I found that this seemed to work for me:

    Dim browser As System.Web.HttpBrowserCapabilities = Request.Browser
    If (browser.Browser = "IE") Then
      Response.AppendHeader("cache-control", "private") ' ie only
    Else
      Response.AppendHeader("cache-control", "no-cache") ' all others (FF/Chrome tested)
    End If
    
    0 讨论(0)
  • 2020-12-08 09:39

    I read of your Cache-control goose chase, but I'll share mine, that met my needs, in case it helps.

    0 讨论(0)
  • 2020-12-08 09:43

    I had a similar problem with PDF files I wanted to stream. Even with Response.ClearHeaders() I saw Pragma and Cache-Control headers added at runtime. The solution was to clear the headers in IIS (Right-click -> Properties on the page loading the PDF, then "Http headers" tab).

    0 讨论(0)
  • 2020-12-08 09:44

    Like the OP I was scratching my head for days trying to get this to work, but I did it in the end so I thought I'd share my 'combination' of headers:

                if (System.Web.HttpContext.Current.Request.Browser.Browser == "InternetExplorer"
                    && System.Web.HttpContext.Current.Request.Browser.Version == "8.0")
                {
                    System.Web.HttpContext.Current.Response.Clear();
                    System.Web.HttpContext.Current.Response.ClearContent();
                    System.Web.HttpContext.Current.Response.ClearHeaders();
                    System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
    
                    System.Web.HttpContext.Current.Response.AppendHeader("Pragma", "public");
                    System.Web.HttpContext.Current.Response.AppendHeader("Cache-Control", "private, max-age=60");
                    System.Web.HttpContext.Current.Response.AppendHeader("Content-Transfer-Encoding", "binary");
    
                    System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + document.Filename);
                    System.Web.HttpContext.Current.Response.AddHeader("content-length", document.Data.LongLength.ToString());
    
                    System.Web.HttpContext.Current.Response.BinaryWrite(document.Data);
                }
    

    Hope that saves someone somewhere some pain!

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