Foren

Dowload files from a Portlet

Nikos Samaras, geändert vor 7 Jahren.

Dowload files from a Portlet

Junior Member Beiträge: 29 Beitrittsdatum: 14.01.16 Neueste Beiträge
Hello,

In my source code I download a file by using ActionRequest and ActionResponse.
Although the file is downloaded correctly there is an issue about the file name.
I can’t set a name to that file. It seems that the system does not care for the setHeader and the setContentType.

Here is my source code

@ActionMapping(params = "action=download")
private void download(ActionRequest actionRequest, ActionResponse actionResponse, Model model, @ModelAttribute("advancedSearchResultForm") DownloadDossierInfo downloadDossierInfo) throws Exception  {        
    final HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);        
    final ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
    String fileName = "test";
    String reportFormat = advanceSearchService.downloadZip(downloadDossierInfo, themeDisplay, response.getOutputStream());
    fillResponseHeaders(response, fileName, reportFormat);       
     response.flushBuffer();        
     response.getOutputStream().flush();
    response.getOutputStream().close();
 }

private void fillResponseHeaders(HttpServletResponse response, String filename, String reportFormat) throws Exception {
        response.setHeader("Content-disposition", "attachment; filename=" + filename + "." + reportFormat.toLowerCase());
        response.setContentType(DownloadFormat.get(reportFormat).getContentType());
    }
 


Please can you help me?
thumbnail
Olaf Kock, geändert vor 7 Jahren.

RE: Dowload files from a Portlet

Liferay Legend Beiträge: 6403 Beitrittsdatum: 23.09.08 Neueste Beiträge
Nikos Samaras:
In my source code I download a file by using ActionRequest and ActionResponse.


This is wrong and not guaranteed to work at all. An ActionResponse must always contain the content that gets embedded into the page (e.g. an HTML fragment), never any independent files.

You'll have to go through a resource handler, e.g. with ResourceRequest/ResourceResponse. During that phase, you will be able to specify all headers you need.