How to Open the word document on browser using the slingservlet

umamaheswariy46

13-03-2019

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

Accepted Solutions (0)

Answers (5)

Answers (5)

Gaurav-Behl

MVP

14-03-2019

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

umamaheswariy46

14-03-2019

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

Gaurav-Behl

MVP

14-03-2019

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  

umamaheswariy46

13-03-2019

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

smacdonald2008

13-03-2019

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