Fórum

How to serve a pdf as attachment

Nikos Samaras, modificado 7 Anos atrás.

How to serve a pdf as attachment

Junior Member Postagens: 29 Data de Entrada: 14/01/16 Postagens Recentes
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, modificado 7 Anos atrás.

Move to more appropiate category

Liferay Legend Postagens: 3089 Data de Entrada: 28/10/08 Postagens Recentes
Move to more appropiate category
thumbnail
Arun Das, modificado 7 Anos atrás.

RE: How to serve a pdf as attachment

Regular Member Postagens: 166 Data de Entrada: 23/07/12 Postagens Recentes
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