Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
how to download pdf file from external site via actionrequest
hi
i'd like to let user read pdf when they click icon
<liferay-ui:icon image="news" message="<%=buttonNameDocInfo%>" url="http://external/?doc=xxx />
it works.
but i'd like to know, how to do it via actionrequest. i changed my code
<portlet:actionURL var="efgpExportDocFunctionURL" name="exportDoc">
<portlet:param name="<%=Constant.EFGP_EXPORT_DOC_NO%>" value="<%=docInfoVO.getDocNo()%>" />
</portlet:actionURL>
<liferay-ui:icon image="news" message="<%=buttonNameDocInfo%>" url="<%=efgpExportDocFunctionURL.toString() %>" />
public void exportDoc(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException {
StringBuffer messageSB = new StringBuffer();
User currentUser = (User) actionRequest.getAttribute(WebKeys.USER);
String docNo = ParamUtil.getString(actionRequest, Constant.EFGP_EXPORT_DOC_NO);
try {
StringBuffer docLink = new StringBuffer();
docLink.append("http://").append(Constant.EFGP_DOC_SERVER).append("/?docNumber=").append(docNo);
LOGGER.info(docLink.toString());
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(docLink.toString());
HttpResponse responset = httpClient.execute(httpGet);
HttpEntity entity = responset.getEntity();
HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
response.setContentType(Constant.HTTP_HEADERS_CONTENT_TYPE_APPLICATION_PDF);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, Constant.HTTP_HEADERS_CONTENT_DISPOSITION_CONTENT_PREFIX_ATTACHMENT_FILENAME + StringPool.QUOTE + docNo + ".PDF" + StringPool.QUOTE);
OutputStream out = response.getOutputStream();
entity.writeTo(out);
out.flush();
out.close();
} catch (Exception ex) {
LOGGER.equals(ex);
messageSB.append("hit exception:").append(ex.getClass().getName()).append(" message:").append(ex.getMessage()).append(Constant.HTML_SNAP);
SessionErrors.add(actionRequest, ex.getClass().getName(), ex);
}
}
but it doesn't work, it seens get the file already, but in borwser, it shows
please let me know how to make it right or any suggestion, thank you very much in advance.
At first you should have a look at ResourceRequests. Downloading of files is typically done with ResourceRequests and not with ActionRequests...
hi Dominik
thank you for reply.
yes, you are right.
but i want the pdf directly be opened on a new window, such like target='_blank".
i do not know how to do it.
Hi,
You can use below Utility for fetching the document url and use it in anchor tag with target="blank".
DLURLHelperUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), themeDisplay, StringPool.BLANK)
hi Mohammed
thank you, thank you very much for reply. you really open my eyes.
i don't know this before and i tried. but unfortunately, it seems like download from external site to server first, and display to user.
i do not want to save it. the origional code is
<a href="http://xxx/?doc=<%=docNo%>" target="_blank">
so i make it as
<a href="http://xxx/doc?=<%=docNo%>" target="_blank"><liferay-ui:icon image="news" message="<%=buttonNameDocInfo%>" /></a>
it works now.
Powered by Liferay™