i've got an error!
Cannot forward after response has been committed!
try {
downloadFile (((HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse()),new File(path));
getFacesContext().getCurrentInstance().responseComplete();
} catch (IOException ex) {
ex.printStackTrace();
}
and the download-method:
public static void downloadFile(
HttpServletResponse response, File file)
throws IOException
{
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Wrap the file in a BufferedInputStream and pass it through another method.
input = new BufferedInputStream(new FileInputStream(file));
//downloadFile(response, input, file.getName(), attachment);
int contentLength = input.available();
String contentType = URLConnection.guessContentTypeFromName(file.getName());
response.setContentLength(contentLength);
response.setContentType(contentType);
response.setHeader(
"Content-disposition", "attachment" + "; filename=\"" + file.getName() + "\"");
output = new BufferedOutputStream(response.getOutputStream());
// Write file contents to response.
while (contentLength-- > 0) {
output.write(input.read());
}
output.flush();
} catch (IOException e) {
throw e;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
main JSF<h:commandButton value="download file" action="#{myBean.action}" onclick="window.open('download.jsf');" />
download.jsf<%
mypackage.MyBean myBean = (mypackage.MyBean) session.getAttribute("myBean");
myBean.downloadFile();
%>
<html>
<head><title>Download</title></head>
<body>Downloading ..</body>
</html>
MyBean (should be session scoped)public void action() {
// Do your thing to handle main JSF during download.
}
public void downloadFile() {
// Stream file to response of download.jsf.
}
What does session.getAttribute("myBean"); do in this example?
Do I have to declare "myBean" somewhere before this line?
When I try to use the code I am getting a sendError(SRTServletResponseContext.java:162)
The first page is working fine but the pop up one shows as an error.
Cheers,
Illu
Got it, thanks!
I've debgged through it and it's going to the right method and everything but it throws an error at the line
if (!ctx.getResponseComplete()) {
And also when I comment this out, the next line throws an error:
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
I think it could be something with the FacesContext ctx? Any ideas?
Illu