sling - How to display Excel, PDf files from Webservice on AEM pages -
i'm trying following: response of webservice excel (a separate call pdf) file. need show file link on aem-page, , whne users click link, browser opens (or downloads) file.
use case: on customer page, there section links order history (excel file), invoice(pdf file), products catalog(excel file). clicking on each link, makes call webservice , fetches respective file.
how achieve this?
here's solution:
from ui, submit action sling servlet
<form name="importfileform" method="get" action="/services/getdata"> <input type="submit" title="submit" value="submit" name="bttnaction"> </form>
your servlet class
public class ttigetservlet extends slingallmethodsservlet { @override protected void doget(slinghttpservletrequest request,slinghttpservletresponse response) throws servletexception,ioexception { ... ... string serviceurl = <<< webservice url>>> httpclient httpclient = httpclients.custom().build(); generatefile(serviceurl, httpclient, request, response); requestdispatcher dispatcher = request.getrequestdispatcher("/content/ttii/en/importfiletest.html"); dispatcher.forward(request, response); } }
generate file method pops file download on browser
public static void generatefile(string serviceurl, httpclient httpclient, slinghttpservletrequest httprequest, slinghttpservletresponse httpresponse) throws clientprotocolexception, ioexception { httpresponse response; httpget httpget = new httpget(serviceurl); // makes call webservice response = httpclient.execute(httpget); // core logic if (response!=null) { contenttype contenttype = contenttype.getordefault(response.getentity()); string mimetype = contenttype.getmimetype(); if (mimetype.equals(mimetype_json)) { // out of context here... } else { // show file servletoutputstream sos = httpresponse.getoutputstream(); httpresponse.setcontenttype("application/vnd.ms-excel"); httpresponse.setheader("content-disposition", "attachment;filename=test.xls"); bufferedhttpentity buf = new bufferedhttpentity(response.getentity()); inputstream istream = buf.getcontent(); sos.write(filehelper.writefiles(istream)); sos.flush(); } }
}
Comments
Post a Comment