This content has been marked as final.
Show 2 replies
-
1. Re: ajax4jsf can not working with the download files
jmiguel77 Aug 17, 2007 5:22 PM (in response to icess)From wich component are you calling this download code ???
I have something similar; in a <a4j:form> i have a commandLink wich actionListener uses a very similar code for downloading a file
the thing is that i use an h:commandLink not an a4j:commandLink
This is my code:FacesContext facesContext = FacesContext.getCurrentInstance(); File file = new File(documentPath); BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); byte[] buf = new byte[1024]; long length = file.length(); if (!facesContext.getResponseComplete()) { String fileName = file.getName(); String contentType = documento.getMimeType(); HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); response.setContentType(contentType); response.setHeader("Content-Disposition","attachment;filename=\"" + fileName + "\""); ServletOutputStream out = response.getOutputStream(); response.setContentLength((int)length); while ((in != null) && ((length = in.read(buf)) != -1)) { out.write(buf, 0, (int)length); } in.close(); out.close(); facesContext.responseComplete();
Hope it helps -
2. Re: ajax4jsf can not working with the download files
maksimkaszynski Aug 20, 2007 6:50 AM (in response to icess)I agree with jmiguel77.
You have to callfacesContext.responseComplete();
if you send file from faces action.
Otherwise, you will have components rendered after file-sending code.
Exception you catch is most likely caused by components trying to render via closed response stream.