views:

352

answers:

1

Hi all,

This is in continuation with my previous question which was not framed properly.

I have an iframe in a jsp class which is calling a struts2 action class in its src, but instead of opening inside the frame the file is getting downloaded,

Inside file TempContentPage.jsp:

<s:form>
<iframe id="displayFrame" src="ContentPage.action"  width="1000" height="500" FRAMEBORDER="0" value="1">&nbsp;</iframe>
</s:form>

Here is the execute method in the action class ContentPage.java

public String execute() throws IOException {

 Session session = SessionUtil.getSession();
 session.beginTransaction();
 ServletOutputStream out = res.getOutputStream();
 ContentBase cb = new ContentBase();


 String quer = "from ContentBase cb where cb.parentType=? AND cb.parentId=? ";
    Query query = session.createQuery(quer);

    query.setParameter(0, "FILE");
    query.setParameter(1, "1");

    list = (ArrayList) query.list();

     if (null != list && !((java.util.ArrayList) list).isEmpty()) {
   cb = (ContentBase) ((java.util.ArrayList) list).get(0);
     }

     docContent = cb.getFile();
     res.reset();
     res.setContentType("application/msword");
     res.setHeader("Content-disposition", "inline; filename=\"scovr.doc\"");


     try{
     InputStream in = docContent.getBinaryStream();

     //InputStream iStream =  new ByteArrayInputStream (docContent.getBytes(0, (int) docContent.length()));

     int length = (int) docContent.length();
     int bufferSize = 1024;
     byte[] buffer = new byte[bufferSize];
        while ((length = in.read(buffer)) != -1) {
         out.write(buffer, 0, length);

        }

 player.setIsRead(true);

  in.close();

     }catch(Exception e){

      e.printStackTrace();
     }

 out.flush();
 return SUCCESS;
}

And here is the struts.xml mapping

<action name="ContentPage" class="com.zoran.action.ContentPage">
        <result name="success" type="stream">
           <param name="contentType">application/msword</param>
           <param name="inputName">in</param>
           <param name="bufferSize">1024</param>
           <param name="contentDisposition">inline</param>
          </result>

      <result name="error" >/pages/ContentPage.jsp</result>
      <result name="input" >/pages/ContentPage.jsp</result>
</action>

I want to open the file inside the iframe scope, Please help me out ( I got valuable inputs from Balusc ) hence changes in this code :).

Thanks, Aditya

A: 

Ah, you're using Struts. Then I recommend to look for real Struts file download example here.

However, you're also dependent on the client (webbrowser) used if it supports opening a MS Word document inline. I have never seen nor tried it, but think (and won't be surprised if) the support is limited to MSIE. Better use PDF instead, there is very wide webbrowser support to display it inline. You can use iText or OOo to convert DOC to PDF.

Or if it is the intent that the client is needs to be able to edit the document and save back to the server, then opening it inline would make no sense and it would only be more confusing for the client, because there's no way to "save" the very same document on the server side other than saving it to the local disk file sytem and then manually uploading to the server side again.

BalusC
Thanks a lot Balusc. Even in IE its not getting rendered, it just pops up a dialogue box asking whether to save or open the box. I would be happy if it works just in just IE itself. And also additionally there is no intent of providing a editable option to the user. A user uploads a .doc file in another page, and a different user accesses it in the above page.
Aditya R
Then the best option what you have is to convert it to PDF or to live with a download link/button instead of an iframe. Good luck.
BalusC