Iframe not showing object from struts action class

橙三吉。 提交于 2019-12-11 17:24:16

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/1695928/iframe-not-showing-object-from-struts-action-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!