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?

with scott: http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.html/forum__xhh5-objective_therespo.html

here's solution:

  1. from ui, submit action sling servlet

    <form name="importfileform" method="get" action="/services/getdata"> <input type="submit" title="submit" value="submit" name="bttnaction"> </form> 
  2. 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);           } } 
  3. 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

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -