直接上栗子
这是官方文档栗子
1 var myUpload = $(element).upload({ 2 name: 'file', 3 action: '', 4 enctype: 'multipart/form-data', 5 params: {}, 6 autoSubmit: true, 7 onSubmit: function() {}, 8 onComplete: function(response) {}, 9 onSelect: function() {} 10 });
1 $("#button-import").upload({ 2 name: 'upload', // <input name="file" /> 3 action: '${pageContext.request.contextPath}/importSubarea.action', // 提交请求action路径 4 enctype: 'multipart/form-data', // 编码格式 5 autoSubmit: true, // 选中文件提交表单 6 onComplete: function(response) {// 请求完成时 调用函数 7 if(response=="success"){ 8 alert("数据导入成功!"); 9 } 10 } 11 });
应用栗子
1 //为导入按钮,添加一键上传效果 2 $("#import").upload({ 3 //默认name为file 4 action : 'xxx.action', 5 onSelect : function(){ 6 //选中文件后,关闭自动提交 7 this.autoSubmit=fasle; 8 //判断文件格式,以.xls或者.xlsx结尾 9 var filename = this.filename(); 10 var regex = /^.*\.(xls|xlsx)$/; 11 if(regex.test(filename)){ 12 //满足 13 this.submit(); 14 } else { 15 $.messager.alert("警告","只能上传.xls或者.xlsx文件!","warning"); 16 } 17 }, 18 onComplete : function(response){ 19 alert("文件上传成功!"); 20 } 21 });