IE8 问题:PDF 文件显示为内联
我正在从我的服务器下载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加这些标头:
这将强制 Internet Explorer 从服务器下载文件。
Add these headers:
This will force Internet Explorer to download the file from the server.
我花了一天时间才弄清楚问题出在哪里。但最后我明白了。
我不能说Evan Mulawski的回答是错误的。我什至尝试过他的代码。但没有帮助。最后发现文件扩展名丢失了。我刚刚将 .pdf 附加到测试文件中。
现在我删除了以下内容。
即使使用上面的代码,我仍然收到 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.
Now I removed the following.
Even with the above code still I am getting the PDF as an attachment.
我同意多路复用器的观点。
实际上,问题在于,如果“文件名”不以与 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.