Im trying to achieve the same download file through JSF as described in those posts: JSF2 download file returns xhtml page source How to provide a file download from a JSF backing bean? http://bharatonjava.wordpress.com/2013/02/01/downloading-file-in-jsf-2/ http://prabinhada.blogspot.com/2012/06/how-to-download-file-using-jsf.html
My problem is that I actually can download the file, but its name is just wrong ( on whichever browser ). It's a XML file I marshall in the download method but browser always receives a file index.jsf ( but mime type is set at text/xml and file size correct )
It runs on:
- Mojarra 2.1.7
- Richfaces 4.3.3.Final
- jboss-7.1.1.Final
Please see below if you spot any problem
public void exportEDL() throws Exception {
String name = this.file.getName();
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
BufferedOutputStream output = null;
StringWriter sw = null;
try {
...
sw = new StringWriter();
// edl is the jaxb i want to return as xml file
marshaller.marshal(this.edl, sw);
// Init servlet response.
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType("text/xml");
response.setContentLength( sw.getBuffer().length() );
response.setHeader("Content-Disposition", "attachment;filename\"" + name + "\"");
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
output.write(sw.toString().getBytes("UTF-8"));
// Finalize task.
sw.flush();
output.flush();
} catch ( Exception e ) {
FacesMessage msg = new FacesMessage( FacesMessage.SEVERITY_ERROR, null, e.getMessage() );
facesContext.addMessage("Error", msg);
return;
} finally {
// Gently close streams.
IOUtil.close(sw);
IOUtil.close(output);
clearUploadData();
}
// Inform JSF that it doesn't need to handle response.
facesContext.responseComplete();
// facesContext.renderResponse();
// facesContext.release();
}
I tried with something else than a XML marshalling without better results. Have you guys manage to download a file with correct name + extension?
It's a commandButton in the view:
<h:commandButton action="#{fileUploadBean.exportEDL()}"
Thanks!