-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.js
375 lines (347 loc) · 15.4 KB
/
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*通过fileExt可以直接指定允许的后缀,通过fileType则可以批量指定允许的后缀,但需要注意指定fileType的时候不会提示允许上传的文件类型,请在placeholder中说明下*/
var UPLOAD_FILE_TYPE_INFO = {
// 图片
'image': ['.jpeg','.jpg','.bmp','.gif','.png','.tif','.rgb','.dib','.eps','.jpe','.pcx','.bmp','.gif','.pdf'],
// 只允许图片(有此属性时不会再判断其它属性,哪怕逗号分隔定义了多个也不行)
'only-image': ['.jpeg','.jpg','.bmp','.gif','.png','.tif','.rgb','.dib','.eps','.jpe','.pcx','.bmp','.gif','.pdf'],
// 文档
'doc': ['.doc', '.docx', '.pdf'],
// 只允许文档(有此属性时不会再判断其它属性,哪怕逗号分隔定义了多个也不行)
'only-doc': ['.doc', '.docx', '.pdf'],
// excel
'excel': ['.xls', '.xlsx', '.pdf']
};
// IE8/9下使用表单提交,需要设置特殊字段为true。后端将在返回的json后插入script,实现跨域
var ADD_SCRIPT = document.documentMode && document.documentMode < 10
? 'true'
: '';
/**
* 初始化upload上传
* @param param {object}
* uploadId ID/HTMLElement
* imgId 上传成功后 显示上传图片的 img标签的ID
* filePathId 上传成功后 文件存储路径
* webFilePath 上传服务主域名
* webViewPath 文件服务主域名
* fileType 文件类型 可选 ("file","image","doc") 多个可使用 "," 分隔
* @returns {boolean}
*/
function fileUpload(param) {
var token = '';
var language = 'cn';
var domain = '';
var _fileObj = {};
var defaults = {
uploadId: '',
imgId: '',
filePathId: '',
webFilePath: window.WEB_FILE_PATH || '',
webViewPath: window.WEB_VIEW_PATH || '',
fileType: 'image',
fileSize: 5,
fileQueueAuto: true,
fileMulti: false,
maxImageSize: 200,
fileExt: null,
callback: null,
onSelect: null,
onError: null,
onComplete: null,
// 弃用api
ifyWidth: 50,
ifyHeight: 31,
buttonImg: '',
fileQueue: 'fileQueue',
queueSizeLimit: 6,
simUploadLimit: 1,
// 新插件参数
defaultWatermark: false, // 默认水印开关("defaultWatermark":true)。上传后默认水印将出现在图片的中央
watermarkText: '', // 提供水印文字(watermarkText)。上传后水印文字将出现在图片的正中
watermarkImage: '', // 提供水印图片的URL(watermarkImage)。上传后水印图片将出现在图片的中央。
fileInputTitle: '请选择'
};
var option = $.extend({}, defaults, param);
var button = typeof option.uploadId === 'string'
? document.getElementById(option.uploadId)
: $(option.uploadId)[0]; // jquery包装一层即可支持传递jquery包装的DOM
if (!$(button).length) {
return false;
}
qq.log(button.nodeName, option.uploadId);
var buttonNodeName = button.nodeName.toLowerCase();
button = (buttonNodeName === 'input' || buttonNodeName === 'button')
? button.parentNode
: button;
$.getJSON('/getUploadToken?t=' + (+new Date()), function(data) {
data = typeof data === 'string' ? JSON.parse(data) : data;
if (data.uploadToken) {
token = data.uploadToken;
language = data.language;
domain = data.domain;
new qq.FineUploaderBasic({
request: {
endpoint: option.endpoint || option.webFilePath + '/upload.do?action=upload',
params: {
moduleFlag: 'report',
token: token,
domain: domain,
language: language,
fileType: option.fileType,
fileSize: option.fileSize,
defaultWatermark: option.defaultWatermark,
maxImageSize: option.maxImageSize,
watermarkText: option.watermarkText,
watermarkImage: option.watermarkImage,
addScript: ADD_SCRIPT
},//后台参数 json格式
uuidName: 'uuid'
},
autoUpload: option.fileQueueAuto,
button: button,
// element: document.getElementById(option.uploadId),
multiple: option.fileMulti,
text: {
fileInputTitle: option.fileInputTitle
},
display: {
prependFiles: true
},
failedUploadTextDisplay: {
mode: 'custom'
},
retry: {
enableAuto: false
},
chunking: {
enabled: false
},
cors: {
allowXdr: true,
expected: true
// sendCredentials: true
},
resume: {
enabled: false
},
callbacks: {
onError: function(id, name, responseJSON, xhr) {
qq.log('=============onError', arguments);
if (isFunction(option.onError)) {
option.onError(responseJSON, id, name, this, xhr);
}
},
// return false 不触发 onSubmitted 回调
onSubmit: function(id, name) {
qq.log('=============onSubmit', arguments);
var fileObj = this.getFile(id) || {
name: this.getName(id),
size: this.getSize(id)
};
qq.log(fileObj);
_fileObj.size = fileObj.size;
_fileObj.type = '.' + fileObj.name.split('.').pop();
if (isFunction(option.onSelect)) {
var s = option.onSelect(fileObj, id, name, this);
if (s === false) {
return false;
}
}
if ($('.file-item').find('p').length >= 5) {
// layer.alert('Maximum allowed upload 5 attached', { icon: 2 });
layer.alert('上传的文件个数不得超过5个', { icon: 2 });
return false;
}
if (fileObj.size > option.fileSize * 1024 * 1024) {
layer.alert('上传文件不允许大于' + option.fileSize + 'M', { icon: 2 });
return false;
}
if (!validateFileType(fileObj, option)) {
return false;
}
return true;
},
/**
* https://docs.fineuploader.com/branch/master/api/events.html#complete
* Integer id
* The current file's id.
*
* String name
* The current file's name.
*
* Object responseJSON
* The raw response from the server.
*
* XMLHttpRequest or XDomainRequest xhr
* The object used to make the request.
*/
onComplete: function(id, name, responseJSON, xhr) {
qq.log('=============onComplete', arguments);
if (isFunction(option.onComplete)) {
var s = option.onComplete(responseJSON, id, name, this, xhr);
if (s === false) {
return false;
}
}
var obj = responseJSON;
var self = this;
$.cookie('JSESSIONID', token);//跨域传输之后必须设置cookie 否则会丢失此次的session
$.getJSON('/getFile?uuid=' + obj.fileUUIDs[0], function(data) {
var callback = option.callback;
var obj = typeof data === 'string' ? JSON.parse(data) : data;
obj.size = _fileObj.size;
obj.type = _fileObj.type;
obj.imgId = option.imgId;
obj.filePathId = option.filePathId;
//执行回调函数 判断是否有回调函数,如果没有就默认执行
if (callback && callback != undefined && typeof callback === 'string') {
callback = window[callback];
if (typeof callback === 'function') {
callback.apply([], [obj, id, name, self]);
}
} else if (isFunction(callback)) {
//判断是否是function
callback(obj, id, name, self);
} else {
//判断是否是图片格式
if (!$.inArray(obj.type, UPLOAD_FILE_TYPE_INFO['image']) >= 0) {
if (obj && obj.resultCode == 4) {
var filePaths = obj.filePaths;//文件查看路径
for (var i = 0; i < filePaths.length; i++) {
$('#' + option.imgId).attr('src', option.webViewPath + '/' + filePaths[i]);
$('#' + option.filePathId).val(filePaths[i]);
}
} else {
layer.alert(obj.resultMsg, { icon: 2 });
}
}
}
});
},
// Called just before an item begins uploading to the server.
onUpload: function(id, name) {
qq.log('=============onUpload', arguments);
}
}
});
}
});
}
function validateFileType(fileObj, settings) {
var objType = '.' + fileObj.name.toLowerCase().split('.').pop();
var fileExt = settings.fileExt || '';
var fileType = settings.fileType || 'image';
if (fileExt) {
var ret = false;
fileExt = fileExt.toLowerCase();
var extArr = fileExt.split(',');
for (var i = 0; extArr && i < extArr.length; i++) {
if (objType == extArr[i]) ret = true;
}
if (!ret) {
layer.alert('请上传文件类型为 ' + fileExt + ' 的文件', { icon: 2 });
return false;
}
return ret;
} else if (fileType) {
var fileTypes = fileType.split(',');
for (var i = 0; i < fileTypes.length; i++) {
var accepts = UPLOAD_FILE_TYPE_INFO[$.trim(fileTypes[i])];
if ($.inArray(objType, accepts) >= 0) {
return true;
}
}
layer.alert('文件类型不正确', { icon: 2 });
return false;
} else {
// 默认只允许图片
if (!$.inArray(objType, UPLOAD_FILE_TYPE_INFO['image']) >= 0) {
layer.alert('文件类型不正确', { icon: 2 });
return false;
}
}
return true;
}
function isFunction(fn) {
return Object.prototype.toString.call(fn) === '[object Function]';
}
/**
* Create a cookie with the given name and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value.
*
* @param String name The name of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
* require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
/**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
var path = options.path ? '; path=' + options.path : '';
var domain = options.domain ? '; domain=' + options.domain : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};