Monday, March 11, 2013

Primefaces fileDownload in Liferay

When I tried to use <p:fileDownload /> in Liferay, with the simplest code shown in the demo site.
primefaces fileDownload code snippet
But the file is failed to download.
To resolve this issue, I ended up with the following steps.
Notes: issue reported in Liferay -

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
 HttpServletResponse res = PortalUtil
 res.setHeader("Content-Disposition", "attachment; filename=\""
   + "downloaded-kian.png" + "\"");//
 res.setHeader("Content-Transfer-Encoding", "binary");

 // 3. write the file into the outputStream
 OutputStream out = res.getOutputStream();
 byte[] buffer = new byte[4096];
 int bytesRead;
 while ((bytesRead = != -1) {
  out.write(buffer, 0, bytesRead);
  buffer = new byte[4096];

 // 4. return null to this method
 return null;



  1. There is no view calling code for this logic.

    1. the view code is straight forward:
      just access the getFile property in managedBean directly

  2. Thanks, you've made ​​my day. Works perfectly!!

  3. I 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.

    However, 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.



  4. Hello. 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:



Related Posts Plugin for WordPress, Blogger...