掲示板

Dowload files from a Portlet

7年前 に Nikos Samaras によって更新されました。

Dowload files from a Portlet

Junior Member 投稿: 29 参加年月日: 16/01/14 最新の投稿
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
7年前 に Olaf Kock によって更新されました。

RE: Dowload files from a Portlet

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
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.