1、XML配置文件
<action name="uploadexc" method="uploadExcel" class="com.maixin.code.action.UserWorkTimeAction" >
<interceptor-ref name ="fileUploadStack" >
<!-- <param name="allowedTypes">xls,xlsx</param> -->
<param name="maximumSize">20000000000</param>
</interceptor-ref>
<result type='json'>
<param name="contentType">text/html</param>
<param name="excludeProperties">upload,userWorkTimeSer</param>
</result>
</action>
2、action代码
复制代码 代码示例:
/**
* 上传Excel
* @return
*/
public String uploadExcel(){
try {
Date d = new Date();
System.out.println("uploadFileName = "+this.uploadFileName);
//upload -- wapps 下面的文件夹,用来存放图片
String toSrc = ServletActionContext.getServletContext().getRealPath("upload")+"/"+d.getTime()+getFileExp(this.uploadFileName); //使用時間戳作為文件名
System.out.println("toFile= "+toSrc);
File toFile = new File(toSrc);
writeFile(this.upload,toFile);
userWorkTimeSer.saveExcelWorkTime(upload);
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
3、js代码
复制代码 代码示例:
this.uploadform.getForm().submit({
url: 'uploadexc.action',
//waitMsg: 'Uploading your photo...',
success: function(response){
},
failure:function(response){
//上传完后,返回失败,
Ext.getCmp('upwindow').hide();
Ext.getCmp('userWorkTimeGrid').getStore().reload();
}
});
在Struts2 中,上传完成后,返回的JSON时,提示下载,用上面的配置后,不会提示了,Ajax返回的居然是failure,使用fileBug查看也是正常的。
好在问题解决了,真是个奇怪的问题。