This commit is contained in:
贤心
2022-05-18 22:35:13 +08:00
parent ce805ee6b3
commit 902aea37d9
49 changed files with 861 additions and 613 deletions

View File

@@ -1,7 +1,6 @@
/*!
* upload 文件上传组件
* MIT Licensed
/**
* upload
* 文件上传组件
*/
layui.define('layer' , function(exports){
@@ -63,6 +62,7 @@ layui.define('layer' , function(exports){
,auto: true //是否选完文件后自动上传
,bindAction: '' //手动上传触发的元素
,url: '' //上传地址
,force: '' //强制规定返回的数据格式,目前只支持是否强制 json
,field: 'file' //文件字段名
,acceptMime: '' //筛选出的文件类型,默认为所有文件
,method: 'post' //请求上传的 http 类型
@@ -218,9 +218,9 @@ layui.define('layer' , function(exports){
allDone();
}
//异常回调
,error: function(){
,error: function(e){
aborted++;
that.msg('请求上传接口出现异常');
that.msg('Request URL is abnormal: '+ (e.statusText || 'error'));
error(index);
allDone();
}
@@ -256,7 +256,7 @@ layui.define('layer' , function(exports){
try {
res = iframeBody.text();
} catch(e) {
that.msg('获取上传后的响应信息出现异常');
that.msg('Cross-domain requests are not supported');
clearInterval(Class.timer);
error();
}
@@ -272,14 +272,18 @@ layui.define('layer' , function(exports){
,done = function(index, res){
that.elemFile.next('.'+ ELEM_CHOOSE).remove();
elemFile.value = '';
if(typeof res !== 'object'){
try {
res = JSON.parse(res);
} catch(e){
res = {};
return that.msg('请对上传接口返回有效JSON');
if(options.force === 'json'){
if(typeof res !== 'object'){
try {
res = JSON.parse(res);
} catch(e){
res = {};
return that.msg('Please return JSON data format');
}
}
}
typeof options.done === 'function' && options.done(res, index || 0, function(files){
that.upload(files);
});
@@ -352,6 +356,14 @@ layui.define('layer' , function(exports){
ajaxSend();
}
//文件类型名称
,typeName = ({
file: '文件'
,images: '图片'
,video: '视频'
,audio: '音频'
})[options.accept] || '文件';
//校验文件格式
value = value.length === 0
@@ -359,39 +371,45 @@ layui.define('layer' , function(exports){
: value;
if(value.length === 0) return;
//根据文件类型校验
switch(options.accept){
case 'file': //一般文件
if(exts && !RegExp('\\w\\.('+ exts +')$', 'i').test(escape(value))){
that.msg('选择的文件中包含不支持的格式');
return elemFile.value = '';
}
layui.each(value, function(i, item){
if(exts && !RegExp('.\\.('+ exts +')$', 'i').test(escape(item))){
return check = true;
}
});
break;
case 'video': //视频文件
if(!RegExp('\\w\\.('+ (exts || 'avi|mp4|wma|rmvb|rm|flash|3gp|flv') +')$', 'i').test(escape(value))){
that.msg('选择的视频中包含不支持的格式');
return elemFile.value = '';
}
layui.each(value, function(i, item){
if(!RegExp('.\\.('+ (exts || 'avi|mp4|wma|rmvb|rm|flash|3gp|flv') +')$', 'i').test(escape(item))){
return check = true;
}
});
break;
case 'audio': //音频文件
if(!RegExp('\\w\\.('+ (exts || 'mp3|wav|mid') +')$', 'i').test(escape(value))){
that.msg('选择的音频中包含不支持的格式');
return elemFile.value = '';
}
layui.each(value, function(i, item){
if(!RegExp('.\\.('+ (exts || 'mp3|wav|mid') +')$', 'i').test(escape(item))){
return check = true;
}
});
break;
default: //图片文件
layui.each(value, function(i, item){
if(!RegExp('\\w\\.('+ (exts || 'jpg|png|gif|bmp|jpeg$') +')', 'i').test(escape(item))){
check = true;
if(!RegExp('.\\.('+ (exts || 'jpg|png|gif|bmp|jpeg') +')$', 'i').test(escape(item))){
return check = true;
}
});
if(check){
that.msg('选择的图片中包含不支持的格式');
return elemFile.value = '';
}
break;
}
//校验失败提示
if(check){
that.msg('选择的'+ typeName +'中包含不支持的格式');
return elemFile.value = '';
}
//检验文件数量
that.fileLength = function(){
var length = 0
@@ -401,8 +419,9 @@ layui.define('layer' , function(exports){
});
return length;
}();
if(options.number && that.fileLength > options.number){
return that.msg('同时最多只能上传的数量为:'+ options.number);
return that.msg('同时最多只能选择 '+ options.number + ' 个文件');
}
//检验文件大小
@@ -417,7 +436,7 @@ layui.define('layer' , function(exports){
limitSize = size;
}
});
if(limitSize) return that.msg('文件不能超过'+ limitSize);
if(limitSize) return that.msg('文件大小不能超过 '+ limitSize);
}
send();
};