【发布时间】: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