Reputation: 641
In my servlet I am using the code below to open a PDF file in a browser, but instead, it shows a download dialog box.
What I am doing wrong?
response.setContentType("application/pdf");
out = response.getWriter();
String filepath = "D:/MyFolder/PDF/MyFile.pdf";
response.setHeader("Content-Disposition", "inline; filename=" + filepath + ";");
FileOutputStream fileOut = new FileOutputStream("D:/MyFolder/PDF/MyFile.pdf");
fileOut.close();
out.close();
Upvotes: 6
Views: 23060
Reputation: 26
u can try to do the same with
response.setHeader("Content-Disposition", "attachment;filename="+filepath+";");
Upvotes: 1
Reputation: 96
you need this:
response.setContentType("application/pdf")
response.setHeader("Content-Disposition", "inline; filename= .. " )
Otherwise, the browser will prompt you to open/save. ( if content type is octet-stream, or content-disposition is attachment )
if you want the pdf to be displayed in a tab, you need to set target = "_blank" in the html ( or angular, jsp, whatever framework you are using ).
Upvotes: 2
Reputation: 119
This is related
Is your browser Firefox? This might be relevant
Upvotes: 0
Reputation: 861
As you have to set the response type with the following configuration:-
File outPutFile=new File(generatedFile);
stream = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
response.setContentLength((int) outPutFile.length());
Upvotes: 7