IHttpHandler响应.ContentType = "application/pdf"在IE8

本文关键字:quot pdf IE8 application ContentType IHttpHandler 响应 | 更新日期: 2023-09-27 18:18:35

这在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;
       }
   }
}

IHttpHandler响应.ContentType = "application/pdf"在IE8

解决!这取决于浏览器的配置。

@BrianRogers -感谢测试这个。我试了"window.location"。href = 'ShowPDF.ashx';"当您这样做时,IE8显示一个空白页面。这导致我质疑我的浏览器配置。我卸载了Foxit Reader,安装了Adobe Reader。现在一切正常

令人困惑的部分是,当我把渲染pdf的代码放在aspx服务器端按钮点击时,IE8显示pdf很好!去图!因此,我之前没有质疑我的浏览器配置。