I want to download a document file by row click on primefaces datatable. I tried to call the action from Bean by using ajax like this:
<p:dataTable
id="docId"
value="#{testBean.document}"
var="doc"
selectionMode="single"
selection="#{testBean.selectedDoc}"
>
<p:ajax event="rowSelect" listener="#{testBean.actionDownload}"/>
<p:column>
...
</p:column>
...
</p:dataTable>
But the following code does not execute what I want. The logic of my action is correct. But seems so that the downloading document does not work with ajax Request. There is no reaction and the download does not executed.
public String actionDownload() {
try {
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
StringBuffer contentDisposition = new StringBuffer();
contentDisposition.append("attachment;");
contentDisposition.append("filename=\"");
contentDisposition.append(name);
contentDisposition.append("\"");
response.reset();
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", contentDisposition.toString());
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/x-download");
out.write(output);
out.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
FacesContext.getCurrentInstance().responseComplete();
return null;
}
Is there an alternative to download a file by click on Datatable row?
actionDownload()
getting invoked? – Selaronsystem.out.println
in there – Kukeltje