【问题标题】:IHttpHandler Response.ContentType = "application/pdf" broken in IE8IHttpHandler Response.ContentType = "application/pdf" 在 IE8 中损坏
【发布时间】:2023-03-26 08:55:01
【问题描述】:

这适用于 Chrome 和 Firefox,但 IE8 不显示任何内容... 当我在 webforms 按钮中尝试相同的代码时,单击它可以在所有三个浏览器上运行。

我怎样才能让它在 IE8 中工作?

public class ShowPDF : IHttpHandler
{

   public void ProcessRequest(HttpContext context)
   {
       // create PDF document
       var document = new PdfDocument();
       var page = document.AddPage();
       var font = new XFont("Verdana", 20, XFontStyle.Bold);
       var gfx = XGraphics.FromPdfPage(page);
       gfx.DrawString("Hello, World!", font
           , PdfSharp.Drawing.XBrushes.Black
           , new PdfSharp.Drawing.XRect(0, 0, page.Width, page.Height)
           , PdfSharp.Drawing.XStringFormats.Center
       );

       // Send PDF to browser
       var stream = new System.IO.MemoryStream();
       document.Save(stream, false);
       context.Response.Clear();
       context.Response.ContentType = "application/pdf";
       context.Response.AddHeader("content-length", stream.Length.ToString());
       context.Response.BinaryWrite(stream.ToArray());
       context.Response.Flush();
       stream.Close();
       context.Response.End();
   }

   public bool IsReusable
   {
       get
       {
           return false;
       }
   }
}

【问题讨论】:

  • 如果代码适用于 FF 和 Chrome,则问题很可能是 application/pdf mime 类型未在 IE 中注册。您可以从 IE 访问其他 PDF 链接吗?
  • 是的,当我把它放在一个 aspx 按钮点击中时,它在 IE8 中工作得很好。当我将它放入页面加载时,它什么也不显示。
  • “放入页面加载”是什么意思?你能告诉我们你放在那里的代码片段吗?我在上面尝试了您的代码(没有 PdfSharp 组件,因此我替换了我已经拥有的实际 PDF 并将其加载到 MemoryStream 中),它在 Windows XP 上的 IE 8 中运行良好。我从表单上的按钮触发下载,其中 onclick 执行“window.location.href = 'ShowPDF.ashx';”

标签: c# asp.net pdf content-type


【解决方案1】:

解决了!它归结为浏览器配置。

@BrianRogers - 感谢您对此进行测试。我试过“window.location.href = 'ShowPDF.ashx';”正如你所做的那样,IE8 显示了一个空白页。这让我质疑我的浏览器配置。我卸载了 Foxit Reader 并安装了 Adob​​e Reader。现在一切正常。

令人困惑的部分是,当我将渲染 pdf 的代码放入 aspx 服务器端按钮单击时,IE8 显示 PDF 就好了!去搞清楚!出于这个原因,我之前没有质疑我的浏览器配置。

【讨论】:

    猜你喜欢
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 2016-08-20
    • 2022-01-14
    • 2013-01-04
    • 1970-01-01
    相关资源
    最近更新 更多