PDF 在浏览器上以文本形式显示
我正在尝试从数据库(byte[])向用户显示 PDF。
我正在使用下面的代码来渲染 PDF。它为我提供了二进制文本形式的 PDF,如下所示。它不是在 PDF 应用程序中打开,而是将 PDF 呈现为文本。
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.AddHeader("Content-Length", fileToDownload.Length.ToString());
//Response.AddHeader("Content-Disposition", "inline; name=RemoteUserGuide.pdf");
Response.AppendHeader("content-length", fileToDownload.Length.ToString());
Response.ContentType = "application/pdf";
Response.BinaryWrite(fileToDownload);
Response.Flush();
////Response.Close();
Response.End();
更新:刚刚注意到,此应用程序页面在 CHROME 中正确呈现 PDF,但仍在 IE 中显示文本。 (服务器上没有 FF3 进行测试)。可能是浏览器的问题?
I am trying to display a PDF from database (byte[]) to user.
I am using code below to render PDF. It is giving me PDF as binary text as shown below. Instead of open in PDF application it is rendering PDF as text.
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.AddHeader("Content-Length", fileToDownload.Length.ToString());
//Response.AddHeader("Content-Disposition", "inline; name=RemoteUserGuide.pdf");
Response.AppendHeader("content-length", fileToDownload.Length.ToString());
Response.ContentType = "application/pdf";
Response.BinaryWrite(fileToDownload);
Response.Flush();
////Response.Close();
Response.End();
UPDATE: Just noticed, this application page is rendering PDF correctly in CHROME, but still displaying text in IE. (don't have FF3 on server to test). Probably its some browser issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
损坏的 adobe 安装会执行此操作。在目标(客户端)计算机上重新安装 Adobe Reader。
有时 Adobe 安装程序会崩溃并执行此操作。
Corrupted adobe install does this. reinstall Adobe Reader on the target (client) machine.
Sometimes the Adobe Installer crashes and does this.
如果您确定您的浏览器可以查看 pdf,那么您应该使用
@PAGE ContentType="application/pdf"
指令而不是Response.ContentType = "application/pdf";
另外,您应该使用 Response.AddHeader("content-disposition", "filename=\"document.pdf\"" ); 来设置文件名
If you are sure, that your browser can view pdf, then you should use
@PAGE ContentType="application/pdf"
directive instead ofResponse.ContentType = "application/pdf";
Also you should use the
Response.AddHeader("content-disposition", "filename=\"document.pdf\"" );
for setting filename确保 IIS(Internet 信息服务)中 .pdf 扩展名的 MIME 类型设置为“application/pdf”。
Make sure your MIME type for the .pdf extension in IIS (Internet Information Services) is set to "application/pdf".
正如其他人所说,尝试将 MIME 类型/标头设置为 application/pdf,如果这不起作用,最后一件事就是关闭浏览器并重新打开同一页面,我之前在使用 FPDF(PHP 中的 PDF 生成库)时遇到过这个问题),看起来浏览器缓存了页面(php)的 MIME 并永远保留它,通过关闭浏览器并重新打开它,它将起作用#1。
As the others said, try setting the MIME type / header to application/pdf, if this doesn't work the last thing is closing your browser and reopening the same page, I had this problem before with FPDF (a PDF generation library in PHP), it looks like the browser cache the MIME of the page (php) and keep it forever, by closing the browser and reopening it, it will work #1.