1.導入jquery.ocupload-1.1.2.js到項目中
2.在頁面引入ocupload.js
<!-- 導入jquery核心類庫 -->
<script type="text/javascript" src="../../js/jquery-1.8.3.js"></script>
<!-- 導入ocupload -->
<script type="text/javascript"
src="../../js/ocupload/jquery.ocupload-1.1.2.js" ></script>
3.爲導入按鈕,添加一鍵上傳效果
// 爲導入按鈕,添加一鍵上傳效果
$("#button-import").upload({
// 默認name爲file
action : '../../area_batchImport.action',
onSelect :function(){
// 選中文件後,關閉自動提交
this.autoSubmit = false ;
// 判定文件格式 ,以.xls 或者 .xlsx 結尾
var filename = this.filename();
var regex = /^.*\.(xls|xlsx)$/ ;
if(regex.test(filename)){
// 滿足
this.submit();
}else{
$.messager.alert("警告","只能上傳.xls或.xlsx結尾的文件!","warning");
}
},
onComplete : function(response){
alert("文件上傳成功!");
}
});
4.使用struts2文件上傳機制,接收上傳文件
編寫AreaAction接收上傳文件,在Action中定義三個成員變量
private File file;
private String fileFileName;
private String fileContentType;