留言板

How to serve a pdf as attachment

Nikos Samaras,修改在7 年前。

How to serve a pdf as attachment

Junior Member 帖子: 29 加入日期: 16-1-14 最近的帖子
Hello

I use Liferay EE and Spring mvc. I try to open a pdf as attachment when I click on download button but the pdf opens in the browses instead of serving an attachment pdf.

It seems that it does not care for

response.setHeader("Content-disposition", "attachment; filename=" + filename + ".pdf");

here is my source code.

@ActionMapping(params = "action=downloadZip")
	private void downloadZip(ActionRequest actionRequest, ActionResponse actionResponse, Model model, @ModelAttribute("dossierView") DossierInfo dossierInfo) throws Exception  {
	    
	  LOG.info("Start downloading zip file");	    
	  
	    final HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
	    
	    String fileName = "test";
	    
	    String reportFormat = dossierViewService.downloadFiles(dossierInfo, response.getOutputStream());
	    
	    response.setHeader("Content-disposition", "attachment; filename=" + filename + ".pdf");
            response.setContentType(("application/pdf"));
	    
	    response.flushBuffer();
	    response.getOutputStream().flush();
}


Please can you help me?

Thank you
Nikos
thumbnail
Juan Gonzalez,修改在7 年前。

Move to more appropiate category

Liferay Legend 帖子: 3089 加入日期: 08-10-28 最近的帖子
Move to more appropiate category
thumbnail
Arun Das,修改在7 年前。

RE: How to serve a pdf as attachment

Regular Member 帖子: 166 加入日期: 12-7-23 最近的帖子
Hi,
Try using serveResource instead of action method.

resourceResponse.setContentType("application/pdf");
resourceResponse.setProperty(HttpHeaders.CONTENT_DISPOSITION,"attachement;filename="+pdfName+".pdf");
			try {
				OutputStream os = resourceResponse.getPortletOutputStream();
				//write your stuff here
				os.close();
				
			} catch (Exception ex) {
				ex.printStackTrace();
			}


Arun