This content has been marked as final.
Show 3 replies
-
1. Re: Force the browser for download prompt
gizola Oct 30, 2008 2:50 AM (in response to pradeepprathyu)
Try to set following:response.setContentType("application/x-download"); response.setHeader("Content-Disposition", "attachment; filename=file.pdf"); response.setHeader("Content-Transfer-Encoding", "application/octet-stream");
This normally forces the browser to save the file rather than open it, like if you set content type to pdf or image or doc, or wathever the browser plugins supports.
Regards,
Gizola -
2. Re: Force the browser for download prompt
pradeepprathyu Oct 30, 2008 4:23 AM (in response to pradeepprathyu)i tried it, but no luck.
-
3. Re: Force the browser for download prompt
gizola Oct 30, 2008 4:38 AM (in response to pradeepprathyu)This is working for me:
FacesContext ctx = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse(); // file should be downloaded without display response.setContentType("application/x-download"); response.setHeader("Content-Disposition", "attachment; filename=file.pdf"); response.setHeader("Content-Transfer-Encoding", "application/octet-stream"); // alternatively you can try: // response.setHeader("Content-Transfer-Encoding", "binary"); FileInputStream fis = null; try { fis = new FileInputStream("e:\\file.pdf"); } catch (FileNotFoundException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } try { response.setContentLength(fis.available()); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } OutputStream out = null; try { out = response.getOutputStream(); } catch (IOException e) { // TODO e.printStackTrace(); // no error message shown to the user. Consider adding a Faces // Message } try { byte b[] = new byte[fis.available()]; fis.read(b); out.write(b); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } ctx.responseComplete(); // this is important to call !!!