应用一键上传的功能,需要导入相应的js文件 jquery.ocupload-1.1.2
简约版
$("#button-import").upload({
action:'../../area_batchImport.action',onSelect:function(){
//获取文件名
var fileName = this.filename();
this.autoSubmit = false;//关掉自动提交
var reg = /^.*\.(xls|xlsx)$/ ;
if(reg.test(fileName)){
this.submit();
}else{
$.messager.alert("警告","请选择正确的文本格式","warning" );
}
},
onComplete:function(response){
alert("上传成功");
}
});
复合版
$(function () {
$(".uploadfile").upload({
action: 'CourseXMLFileUploadHander.ashx',
name: 'xml',
params: {
'type': 'uploadCourseXMLFile',
'rand': Math.random()
},
onSelect: function (self, element) {
this.autoSubmit = false;
var re = new RegExp("(xml){1}quot;, "i");
if (!re.test(this.filename())) {
alert("Only xml file can be uploaded");
}
else {
this.submit();
}
},
onSubmit: function (self, element) {
$('.uploadfile').hide();
$('#ajax_update').parent().show();
//alert('Uploading file...');
},
onComplete: function (data, self, element) {
$('#ajax_update').parent().hide();
$('.uploadfile').show();
self.resetInput();
try {
var ret = data;
if (ret.indexOf("exception") >= 0) {
alert('Upload file exception: ' + eval(data)[0].exception);
}
else {
showSuccess('File is successfully Load.');
uploadSuccess(ret);
}
} catch (err) {
alert(data);
}
}
});
});