RE: Issue with serveResource

Daniel G, modified 6 Years ago. Regular Member Posts: 141 Join Date: 3/14/17 Recent Posts
Dear all,

I am facing an issue when calling ServeResource method.

I want to make a portlet which returns an excel file when a button is pressed. The excel has to be generated each time that the button is pressed.

My problem is that the excel which is returned is always the same, the first one which is generated. Only if I remove the portlet and put it again, it generates a new excel. 

Anyone has any idea of why is this happening?

I use this in serve resource method:

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
res.setContentType("application/vnd.ms-excel");
res.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
ResourceBundle resourceBundle = ResourceBundle.getBundle("content.Language",res.getLocale());
res.addProperty(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+LanguageUtil.get(resourceBundle, "excel.title")+"_"+ System.currentTimeMillis() + ".xls");




Any help would be very appreciated.


Thanks in advance.

Kind regards.
thumbnail
Jorge Díaz, modified 6 Years ago. Liferay Master Posts: 753 Join Date: 1/9/14 Recent Posts
Hi Daniel G,

I implemented that funcionality in following portlet:
Notes:
  1. serveResource  of portlet calls OutputUtils.servePortletFileEntry method
  2. In order to have a resourceId in request.getResourceId() call, you have to set it in the request during previous doView() execution, see lines 443 to 450:
  3. Download link is written here, see lines 240 to 250:

In my portlet I am generating all data in doView method, storing it to document library and in serveResource method I am just getting it and sending to browser, but it could be possible to do all generation code inside serveResource

Regards,
Jorge
thumbnail
Fernando Fernandez, modified 6 Years ago. Expert Posts: 401 Join Date: 8/22/07 Recent Posts
Hi Daniel,

Your serveResource method should take care of regenerating the file whenever it's called. Is it? Not sure, from the code you posted... For that it normally would get some parameters from the caller page, so that the file would be correctly generated.

HTH

Fernando
Daniel G, modified 6 Years ago. Regular Member Posts: 141 Join Date: 3/14/17 Recent Posts
Hi all.

First of all, thanks to both of you. I was able to solve the problem with your help.

My issue was due to that I was calling the method in a wrong way, so the resource stand stuck in the server.

Thanks again!

regards