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 开发者_StackOverflow中文版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();
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.
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.
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.
精彩评论