【问题标题】:Chrome not displaying PDF from asp.net pageChrome 不显示来自 asp.net 页面的 PDF
【发布时间】:2012-07-13 15:05:11
【问题描述】:

.aspx 页面中有一个 .pdf 文件,如下所示:<embed src="http://.../ShowPdf.aspx?id=1" type="application/pdf">。 Chrome 只显示“正在加载”图像并挂起而不显示 pdf(chrome pdf 查看器和 adobe 插件都不起作用)。其他浏览器打开pdf。有什么想法吗?

【问题讨论】:

    标签: .net google-chrome pdf


    【解决方案1】:

    我也遇到过这个问题,经过多次研究终于解决了。

    我发现HTTP响应头应该有以下实体头:

    content-length: file size

    不指定这个头,web服务器会放默认的:

    content-length: chunked

    我不知道为什么谷歌浏览器会出现这个问题,因为正如你所说,在 IE 或 Firefox 等其他浏览器中,它们会正确呈现/打开 PDF 文件。

    以下是我用来解决此问题的代码。它适用于我的情况,现在 Google Chrome 正在显示我的网络应用程序的 PDF 文件!

        System.IO.FileInfo fileInfo;
        fileInfo = My.Computer.FileSystem.GetFileInfo(fullPathToFile);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
    

    希望对你有所帮助。

    以下是我认为有用的一些参考资料:
    PDF Handler Problem with Chrome & Firefox
    What's the “Content-Length” field in HTTP header?

    【讨论】:

      【解决方案2】:

      非常感谢,我也遇到了同样的问题,已经解决了。我使用的是报告查看器,但我需要自动以 pdf 格式显示它,并且它工作正常,但是当我使用 Chrome 时,它​​有同样的问题,当我想保存文件时,它以扩展名 .ASPX 保存而不是 .pdf。

      我与解决方案分享我的代码:

                      string deviceInfo = "";
                      string[] streams = null;
                      string mimeType = null;
                      string encoding = null;
                      string fileNameExtension = null;
                      Warning[] war = null;
                      Byte[] bytes = this.ReportViewer.LocalReport.Render("PDF", deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out war);
                      System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
                      Response.Clear();
                      Response.ContentType = "Application/pdf";
                      Response.AddHeader("Content-Length", bytes.Length.ToString());
                      Response.AddHeader("Content-disposition", "inline; filename= NombreReporte");
                      Response.BinaryWrite(ms.ToArray());
                      Response.Flush();
                      Response.End(); 
      

      【讨论】:

        【解决方案3】:

        如果您也遇到在 Firefox 中显示 PDF 的问题,我发现了这个问题:

        "你需要指定内容类型以便浏览器知道什么文件 它正在下载/显示内联......就像这样:“

             Response.ContentType = "application/pdf"
             Response.AddHeader("Content-Disposition", "inline;filename=YourFileName.pdf")
        

        它对我有用,我也希望对你有帮助。

        来源:http://forums.asp.net/t/1784151.aspx

        【讨论】:

          猜你喜欢
          • 2019-02-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-02
          • 2012-09-16
          • 2016-08-08
          • 1970-01-01
          相关资源
          最近更新 更多