Search code examples
internet-explorer-8pdf-generationinlinedhtmlie8-browser-mode

IE8 Issue: PDF file is displayed as inline


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();

Solution

  • 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.