upload file in struts2 using ajax

后端 未结 2 691
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 09:43

How can I upload a file in struts 2 using ajax

相关标签:
2条回答
  • 2021-01-05 10:13

    If you want to return json data to client, you can get the error that a file download dialog appear to download json as a text file. I use struts2-json plugin so to fix this error, just add the following config to your action in struts.xml

    <param name="contentType">text/plain</param>
    

    my sample

            <action name="uploadFile" class="fileUploadAction">
                <interceptor-ref name="fileUploadStack"/>
                <result name="input">/WEB-INF/pages/uploadForm.jsp</result>
                <result name="success" type="json">
                     <param name="excludeProperties">
                             fileStoreManager, file
                     </param>
                     <param name="contentType">text/plain</param>
                </result>
                <result name="cancel" type="redirectAction">mainMenu</result>
            </action>
    
    0 讨论(0)
  • 2021-01-05 10:19

    Download the Struts2 jQuery Plugin and do it like you normaly do it with Struts2.

    <%@ taglib prefix="s" uri="/struts-tags"%>
    <%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
    <html>
      <head>
        <sj:head/>
      </head>
      <body>
        <s:form id="form" action="AjaxTest">
          <s:file name="myFile" ... />
          <sj:submit value="Submit Form" targets="myAjaxTarget"/>
        </s:form>
        <div id="myAjaxTarget">
        </div>
      </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题