Upload file to struts2 using ajax

How to upload a file in struts 2 using ajax

+7
source share
2 answers

Download the Struts2 jQuery Plugin plugin and do it as usual 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> 
+9
source

If you want to return json data to the client, you may receive an error message that a file download dialog box appears to download json as a text file. I am using struts2-json plugin to fix this error, just add the following configuration to your action in struts.xml

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

my example

  <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> 
+7
source

All Articles