Expand my Community achievements bar.

How to Open the word document on browser using the slingservlet

Avatar

Community Advisor

Hi Team,

Need help on how to open a word document on the browser using the sling servlet.

I am able to open the pdf files by setting the content-disposition as inline but I am not able to open the word docs with the same.

Can anyone have any ideas on how to do it please guide me.

Req is: Need to download the file to users local desktop and open it automatically.

Thanks

Uma

5 Replies

Avatar

Level 10

There is no AEM specific functionality to perform this task. You would use Java logic like you would for any J2EE applications. Use Java logic in the Servlet to write back the WORD DOC in the browser. For example --

How to Open a Word Document From a Servlet Response - MS word Docs Q&A

Avatar

Community Advisor

Thanks

I am able to do until download the file as an attachment word and pdf both.

I am stuck at the last step for the word

I need help for these 2 lines.

URL url = new URL ( res );

url.OpenStream();

Thanks

Uma

Avatar

Level 10

Is your requirement to download the file or open it?

       protected void doGet(SlingHttpServletRequest request,

SlingHttpServletResponse response) throws ServletException,

IOException {

//do something

//get your document stream

response.setContentType("application/pdf"); 

    //response.setHeader("content-disposition","attachment; filename=something"); 

try {

response.getOutputStream().write(byte array);

}catch (JSONException e) {

logger.error("JSONException caught from Exception: "+e.getMessage());

}catch (DocumentException e) {

logger.error(e);

}

}

// url based approach

  URL url = new URL(theUrl);

    URLConnection urlConnection = url.openConnection();

    new InputStreamReader(urlConnection.getInputStream()));

   // do something with reader  

Avatar

Community Advisor

Thanks, Gaurav. I will try the URL part.

I wanted to download from DAm to shared location and open the same document. I am able to open the pdf file using 'discomposition' as 'inline' but I need to open the word as well

Thanks

Uma

Avatar

Level 10

For opening the document, above mentioned code would work

For downloading to shared drive - get the stream > get bytes > use FileOutputStream to save the file to your location