When I tried to use <p:fileDownload /> in Liferay, with the simplest code shown in the demo site.
But the file is failed to download.
To resolve this issue, I ended up with the following steps.
Notes: issue reported in Liferay - http://issues.liferay.com/browse/FACES-1513
Public StreamData getFile() {
// 1. initialize the fileInputStream
// 2. get Liferay's ServletResponse
// 3. write the file into the outputStream
// 4. return null to this method
}
Done!!
But the file is failed to download.
To resolve this issue, I ended up with the following steps.
Notes: issue reported in Liferay - http://issues.liferay.com/browse/FACES-1513
Public StreamData getFile() {
// 1. initialize the fileInputStream
// 2. get Liferay's ServletResponse
// 3. write the file into the outputStream
// 4. return null to this method
}
Sample codes:
public StreamedContent getFile() throws Exception {
// 1. initialize the fileInputStream
File localfile = new File("C:/kian.png");
FileInputStream fis = new FileInputStream(localfile);
// 2. get Liferay's ServletResponse
PortletResponse portletResponse = (PortletResponse) FacesContext
.getCurrentInstance().getExternalContext().getResponse();
HttpServletResponse res = PortalUtil
.getHttpServletResponse(portletResponse);
res.setHeader("Content-Disposition", "attachment; filename=\""
+ "downloaded-kian.png" + "\"");//
res.setHeader("Content-Transfer-Encoding", "binary");
res.setContentType("application/octet-stream");
res.flushBuffer();
// 3. write the file into the outputStream
OutputStream out = res.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
buffer = new byte[4096];
}
// 4. return null to this method
return null;
}
Done!!
There is no view calling code for this logic.
ReplyDeletethe view code is straight forward:
Deletejust access the getFile property in managedBean directly
Thanks, you've made my day. Works perfectly!!
ReplyDeleteI just attached liferay-faces-1513-patch-SNAPSHOT.jar to FACES-1513. This patch should make it possible to use p:fileDownload and p:dataExporter in a portlet without doing any portlet-specific coding in your own code.
ReplyDeleteHowever, there are two limitations:
p:fileDownload – the PrimeFaces.monitorDownload(start, stop) JavaScript API doesn’t call the “stop” function due to cookie issues in a portlet environment.
p:dataExporter – unable to get type=”xml” working but I tried “csv”, “pdf”, and “xls” and they worked fine.
Please give the patch a try and let me know if it works for you. Simply copy it to tomcat/webapps/yourportlet/WEB-INF/lib and restart Tomcat.
Thanks,
Neil
Neil, Don't works!!!
DeleteHello. First of all, thank you for your solution. We have used the workaround you specified in one of our own Liferay projects but we ended up having some memory leak issues. After digging a bit to understand what the problem could be, we realized that it seemed to be related to the bridgeRequestScopeCache and we reported that to Liferay. You may check the full report here: https://issues.liferay.com/browse/FACES-2035
ReplyDelete