PDF 在浏览器上以文本形式显示

发布于 2024-09-04 11:24:20 字数 918 浏览 1 评论 0原文

我正在尝试从数据库(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?

Any Ideas?
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不知所踪 2024-09-11 11:24:20

损坏的 adobe 安装会执行此操作。在目标(客户端)计算机上重新安装 Adob​​e Reader。

有时 Adob​​e 安装程序会崩溃并执行此操作。

Corrupted adobe install does this. reinstall Adobe Reader on the target (client) machine.

Sometimes the Adobe Installer crashes and does this.

吻泪 2024-09-11 11:24:20

如果您确定您的浏览器可以查看 pdf,那么您应该使用 @PAGE Con​​tentType="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 of Response.ContentType = "application/pdf";

Also you should use the Response.AddHeader("content-disposition", "filename=\"document.pdf\"" ); for setting filename

梦里寻她 2024-09-11 11:24:20

确保 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".

傾城如夢未必闌珊 2024-09-11 11:24:20

正如其他人所说,尝试将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文