IE8 问题:PDF 文件显示为内联

发布于 2024-10-05 12:33:31 字数 1038 浏览 0 评论 0原文

我正在从我的服务器下载 PDF。我将“内容处置”设置为“附件”。 Firefox 运行得非常好。但在 IE8 中它显示为内联。有解决此问题的快速指示吗?

编辑:

我正在使用 Springs 编写 PDF 字节数组流。并在客户端使用JSP来显示。

客户端:

我正在使用 dhtml 网格并保留标签。网格中的代码如下所示:

<a href='javascript:viewPDF(14)' target="_self" >View</a>

单击此按钮后,将调用 viewPDF 方法。我将此代码保存在我的 javascript 文件中。

function viewPDF(id) {
    $("#pdfID").val(id);
    $("#myform").attr('action',url);
    $("#myform").submit();
}

服务器端代码库:

ByteArrayOutputStream reportBAOS = getPDFByteArrayStream();/*This is my method which returns the byte array stream.*/
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment; filename=testfile");
response.setHeader("Pragma","Public");
response.setHeader("Cache-Control","must-revalidate,post-check=0,pre-check=0");
response.setHeader("Expires","0");
ServletOutputStream os = response.getOutputStream();
os.write(reportBAOS.toByteArray());
os.flush();
os.close();

I am downloading a PDF from my server. I set the "Content-Disposition" as "attachment". Its working very fine is Firefox. But in IE8 its displayed as inline. Any quick pointers to resolve this issue ?

Edit:

I am using Springs to write the PDF byte array stream. And using JSP in the client side to display.

Client side:

I am using a dhtml grid and keeping an tag. The code in the grid looks like:

<a href='javascript:viewPDF(14)' target="_self" >View</a>

On click of this the method viewPDF gets invoked. I kept this code in my javascript file.

function viewPDF(id) {
    $("#pdfID").val(id);
    $("#myform").attr('action',url);
    $("#myform").submit();
}

Server side code base:

ByteArrayOutputStream reportBAOS = getPDFByteArrayStream();/*This is my method which returns the byte array stream.*/
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment; filename=testfile");
response.setHeader("Pragma","Public");
response.setHeader("Cache-Control","must-revalidate,post-check=0,pre-check=0");
response.setHeader("Expires","0");
ServletOutputStream os = response.getOutputStream();
os.write(reportBAOS.toByteArray());
os.flush();
os.close();

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

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

发布评论

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

评论(3

半枫 2024-10-12 12:33:31

添加这些标头:

header("Pragma: public"); //This one may work by itself.
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("200 HTTP/1.0 OK"); //HTTP 1.0 Compatible

这将强制 Internet Explorer 从服务器下载文件。

Add these headers:

header("Pragma: public"); //This one may work by itself.
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("200 HTTP/1.0 OK"); //HTTP 1.0 Compatible

This will force Internet Explorer to download the file from the server.

盗琴音 2024-10-12 12:33:31

我花了一天时间才弄清楚问题出在哪里。但最后我明白了。

我不能说Evan Mulawski的回答是错误的。我什至尝试过他的代码。但没有帮助。最后发现文件扩展名丢失了。我刚刚将 .pdf 附加到测试文件中。

response.setHeader("Content-Disposition","attachment; filename=testfile.pdf");

现在我删除了以下内容。

response.setHeader("Pragma","Public");
response.setHeader("Cache-Control","must-revalidate,post-check=0,pre-check=0");
response.setHeader("Expires","0");

即使使用上面的代码,我仍然收到 PDF 作为附件。

I spent a day to figure out what was the issue. But finally I got it.

I cannot say the Evan Mulawski's answer wrong. I tried with his code even. But no help. Finally I found that the file name extension is missing. I just appended .pdf to testfile.

response.setHeader("Content-Disposition","attachment; filename=testfile.pdf");

Now I removed the following.

response.setHeader("Pragma","Public");
response.setHeader("Cache-Control","must-revalidate,post-check=0,pre-check=0");
response.setHeader("Expires","0");

Even with the above code still I am getting the PDF as an attachment.

余厌 2024-10-12 12:33:31

我同意多路复用器的观点。
实际上,问题在于,如果“文件名”不以与 Windows 中的 Acrobat Reader 关联的后缀结尾。一旦添加“.pdf”,它就可以正常工作。

还有一个Cache-Control: no-cache 的陷阱,它会导致 IE 呕吐。
使用 Cache-control: private 来防止缓存。

I agree with Multiplexer.
Actually the problem is that if the 'filename' does not end with suffix that is associated with Acrobat Reader in windows. As soon as you add ".pdf" it works ok.

Then theres the pitfal of Cache-Control: no-cache which will cause IE to puke.
Use Cache-control: private to prevent caching.

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