-
Notifications
You must be signed in to change notification settings - Fork 0
/
layui_upload.js
151 lines (129 loc) · 3.9 KB
/
layui_upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*!
@Title: layui.upload 单文件上传 - 全浏览器兼容版
@Author: 贤心
@License:LGPL
*/
var layer = require('layer');
var elemDragEnter = 'layui-upload-enter';
var elemIframe = 'layui-upload-iframe';
var msgConf = {
icon: 2
,shift: 6
}
var fileType = {
file: '文件'
,video: '视频'
,audio: '音频'
};
var Upload = function(options){
this.options = options || {};
this.init();
};
//初始化渲染
Upload.prototype.init = function(){
var that = this, options = that.options;
var body = $('body'), elem = $(options.elem || '.layui-upload-file');
var iframe = $('<iframe id="'+ elemIframe +'" class="'+ elemIframe +'" name="'+ elemIframe +'"></iframe>');
//插入iframe
$('#'+elemIframe)[0] || body.append(iframe);
return elem.each(function(index, item){
item = $(item);
var form = '<form target="'+ elemIframe +'" method="'+ (options.method||'post') +'" key="set-mine" enctype="multipart/form-data" action="'+ (options.url||'') +'"></form>';
var type = item.attr('lay-type') || options.type; //获取文件类型
//包裹ui元素
if(!options.unwrap){
form = '<div class="layui-box layui-upload-button">' + form + '<span class="layui-upload-icon"><i></i>'+ (
item.attr('lay-title') || options.title|| ('上传'+ (fileType[type]||'图片') )
) +'</span></div>';
}
form = $(form);
//拖拽支持
if(!options.unwrap){
form.on('dragover', function(e){
e.preventDefault();
$(this).addClass(elemDragEnter);
}).on('dragleave', function(){
$(this).removeClass(elemDragEnter);
}).on('drop', function(){
$(this).removeClass(elemDragEnter);
});
}
//如果已经实例化,则移除包裹元素
if(item.parent('form').attr('target') === elemIframe){
if(options.unwrap){
item.unwrap();
} else {
item.parent().next().remove();
item.unwrap().unwrap();
}
};
//包裹元素
item.wrap(form);
//触发上传
item.off('change').on('change', function(){
that.action(this, type);
});
});
};
//提交上传
Upload.prototype.action = function(input, type){
var that = this, options = that.options, val = input.value;
var item = $(input), ext = item.attr('lay-ext') || options.ext || ''; //获取支持上传的文件扩展名;
if(!val){
return;
};
//校验文件
switch(type){
case 'file': //一般文件
if(ext && !RegExp('\\w\\.('+ ext +')$', 'i').test(escape(val))){
layer.msg('不支持该文件格式', msgConf);
return input.value = '';
}
break;
case 'video': //视频文件
if(!RegExp('\\w\\.('+ (ext||'avi|mp4|wma|rmvb|rm|flash|3gp|flv') +')$', 'i').test(escape(val))){
layer.msg('不支持该视频格式', msgConf);
return input.value = '';
}
break;
case 'audio': //音频文件
if(!RegExp('\\w\\.('+ (ext||'mp3|wav|mid') +')$', 'i').test(escape(val))){
layer.msg('不支持该音频格式', msgConf);
return input.value = '';
}
break;
default: //图片文件
if(!RegExp('\\w\\.('+ (ext||'jpg|png|gif|bmp|jpeg') +')$', 'i').test(escape(val))){
layer.msg('不支持该图片格式', msgConf);
return input.value = '';
}
break;
}
if (options.before && !options.before(input)) {
return false;
}
item.parent().submit();
var iframe = $('#'+elemIframe), timer = setInterval(function() {
var res;
try {
res = iframe.contents().find('body').text();
} catch(e) {
layer.msg('上传接口存在跨域', msgConf);
clearInterval(timer);
}
if(res){
clearInterval(timer);
iframe.contents().find('body').html('');
try {
res = JSON.parse(res);
} catch(e){
res = {};
return layer.msg('请对上传接口返回JSON字符', msgConf);
}
typeof options.success === 'function' && options.success(res, input);
}
}, 30);
input.value = '';
};
//暴露接口
module.exports = Upload;