I have a PDF file on my server that I need the user to download from the client side.
Using Spring Framework, I use javax.servlet.http.HttpServletResponse to create the correct response and corresponding header:
response.setHeader("Expires", "-1");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename="content.pdf");
response.setContentLength(content.size());
Then I use the ServletOutputStream to write the content:
ServletOutputStream os;
try {
os = response.getOutputStream();
os.write(((ByteArrayOutputStream)baos).toByteArray());
baos.close();
os.flush();
os.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
On the client side, I receive HTTP code 200 and receive the correct response body, with the PDF file, but the 'Save As...' pop-up did not appear.
Is there any reason on the Header configuration that could lead to this problem or could it be somewhere else?
Thank you.
Try setting the content type to
application/octet-stream
instead:This should force the browser to show the "Save As..." popup. If you set it to
application/pdf
the browser recognizes the file type and displays it instead.maybe:
attachment;
spacefilename=content.pdf
update
and the only difference i see is in the headers section. have you tried without cache-control, pragma and expires?
update
no difference at all using a file or a stream:
When I ran this code the problem came in
for defining filename
Try:
It will open up dialog box and will give you save as option when clicking save button