-
1. Re: how to Download / open file using richfaces
pvito Jan 18, 2012 2:29 AM (in response to aniketmurtarkar)Hi, Aniket Murtarkar
I think that code snippet will be help for you
<h:commandLink actionListener="#{reportBean.downloadFile}"/>
public void downloadFile(ActionEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
int read = 0;
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=\"" + "file.pdf" + "\"");
try {
FileInputStream fis = new FileInputStream("file.pdf");
OutputStream os = null;
byte[] bytes1 = new byte[1024];
os = response.getOutputStream();
while ((read = fis.read(bytes1)) != -1) {
os.write(bytes1, 0, read);
}
os.flush();
os.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
addErrMessage(e);
}
FacesContext.getCurrentInstance().responseComplete();
}
Regards, Vitaliy
-
2. Re: how to Download / open file using richfaces
aniketmurtarkar Jan 20, 2012 5:32 AM (in response to pvito)Thank you Vitaliy,
It realy helped me to solve the problem.
only thing I needed to do is that... I have to create a new file and add all data from database and then given it to response object as given by you.
here is the added code.
BufferedOutputStream bos = null;
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(cmdObj.getData());
FileInputStream fis = new FileInputStream(fileName);
and rest as per the your code.
Thanks alot for this great help.