-
Notifications
You must be signed in to change notification settings - Fork 147
/
Q.Uploader.image.all.js
1727 lines (1288 loc) · 56.6 KB
/
Q.Uploader.image.all.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Q.js for Uploader
* author:devin87@qq.com
* update:2017/09/22 14:50
*/
(function (window, undefined) {
"use strict";
var toString = Object.prototype.toString,
has = Object.prototype.hasOwnProperty,
slice = Array.prototype.slice;
//若value不为undefine,则返回value;否则返回defValue
function def(value, defValue) {
return value !== undefined ? value : defValue;
}
//检测是否为函数
function isFunc(fn) {
//在ie11兼容模式(ie6-8)下存在bug,当调用次数过多时可能返回不正确的结果
//return typeof fn == "function";
return toString.call(fn) === "[object Function]";
}
//检测是否为正整数
function isUInt(n) {
return typeof n == "number" && n > 0 && n === Math.floor(n);
}
//触发指定函数,如果函数不存在,则不触发
function fire(fn, bind) {
if (isFunc(fn)) return fn.apply(bind, slice.call(arguments, 2));
}
//扩展对象
//forced:是否强制扩展
function extend(destination, source, forced) {
if (!destination || !source) return destination;
for (var key in source) {
if (key == undefined || !has.call(source, key)) continue;
if (forced || destination[key] === undefined) destination[key] = source[key];
}
return destination;
}
//Object.forEach
extend(Object, {
//遍历对象
forEach: function (obj, fn, bind) {
for (var key in obj) {
if (has.call(obj, key)) fn.call(bind, key, obj[key], obj);
}
}
});
extend(Array.prototype, {
//遍历对象
forEach: function (fn, bind) {
var self = this;
for (var i = 0, len = self.length; i < len; i++) {
if (i in self) fn.call(bind, self[i], i, self);
}
}
});
extend(Date, {
//获取当前日期和时间所代表的毫秒数
now: function () {
return +new Date;
}
});
//-------------------------- browser ---------------------------
var browser_ie;
//ie11 开始不再保持向下兼容(例如,不再支持 ActiveXObject、attachEvent 等特性)
if (window.ActiveXObject || window.msIndexedDB) {
//window.ActiveXObject => ie10-
//window.msIndexedDB => ie11+
browser_ie = document.documentMode || (!!window.XMLHttpRequest ? 7 : 6);
}
//-------------------------- json ---------------------------
//json解析
//secure:是否进行安全检测
function json_decode(text, secure) {
//安全检测
if (secure !== false && !/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]").replace(/(?:^|:|,)(?:\s*\[)+/g, ""))) throw new Error("JSON SyntaxError");
try {
return (new Function("return " + text))();
} catch (e) { }
}
if (!window.JSON) window.JSON = {};
if (!JSON.parse) JSON.parse = json_decode;
//-------------------------- DOM ---------------------------
//设置元素透明
function setOpacity(ele, value) {
if (value <= 1) value *= 100;
if (ele.style.opacity != undefined) ele.style.opacity = value / 100;
else if (ele.style.filter != undefined) ele.style.filter = "alpha(opacity=" + parseInt(value) + ")";
}
//获取元素绝对定位
function getOffset(ele, root) {
var left = 0, top = 0, width = ele.offsetWidth, height = ele.offsetHeight;
do {
left += ele.offsetLeft;
top += ele.offsetTop;
ele = ele.offsetParent;
} while (ele && ele != root);
return { left: left, top: top, width: width, height: height };
}
//遍历元素节点
function walk(ele, walk, start, all) {
var el = ele[start || walk];
var list = [];
while (el) {
if (el.nodeType == 1) {
if (!all) return el;
list.push(el);
}
el = el[walk];
}
return all ? list : null;
}
//获取上一个元素节点
function getPrev(ele) {
return ele.previousElementSibling || walk(ele, "previousSibling", null, false);
}
//获取下一个元素节点
function getNext(ele) {
return ele.nextElementSibling || walk(ele, "nextSibling", null, false);
}
//获取第一个元素子节点
function getFirst(ele) {
return ele.firstElementChild || walk(ele, "nextSibling", "firstChild", false);
}
//获取最后一个元素子节点
function getLast(ele) {
return ele.lastElementChild || walk(ele, "previousSibling", "lastChild", false);
}
//获取所有子元素节点
function getChilds(ele) {
return ele.children || walk(ele, "nextSibling", "firstChild", true);
}
//创建元素
function createEle(tagName, className, html) {
var ele = document.createElement(tagName);
if (className) ele.className = className;
if (html) ele.innerHTML = html;
return ele;
}
//解析html标签
function parseHTML(html, all) {
var box = createEle("div", "", html);
return all ? box.childNodes : getFirst(box);
}
//-------------------------- event ---------------------------
var addEvent,
removeEvent;
if (document.addEventListener) { //w3c
addEvent = function (ele, type, fn) {
ele.addEventListener(type, fn, false);
};
removeEvent = function (ele, type, fn) {
ele.removeEventListener(type, fn, false);
};
} else if (document.attachEvent) { //IE
addEvent = function (ele, type, fn) {
ele.attachEvent("on" + type, fn);
};
removeEvent = function (ele, type, fn) {
ele.detachEvent("on" + type, fn);
};
}
//event简单处理
function fix_event(event) {
var e = event || window.event;
//for ie
if (!e.target) e.target = e.srcElement;
return e;
}
//添加事件
function add_event(element, type, handler, once) {
var fn = function (e) {
handler.call(element, fix_event(e));
if (once) removeEvent(element, type, fn);
};
addEvent(element, type, fn);
if (!once) {
return {
//直接返回停止句柄 eg:var api=add_event();api.stop();
stop: function () {
removeEvent(element, type, fn);
}
};
}
}
//触发事件
function trigger_event(ele, type) {
if (isFunc(ele[type])) ele[type]();
else if (ele.fireEvent) ele.fireEvent("on" + type); //ie10-
else if (ele.dispatchEvent) {
var evt = document.createEvent("HTMLEvents");
//initEvent接受3个参数:事件类型,是否冒泡,是否阻止浏览器的默认行为
evt.initEvent(type, true, true);
//鼠标事件,设置更多参数
//var evt = document.createEvent("MouseEvents");
//evt.initMouseEvent(type, true, true, ele.ownerDocument.defaultView, 1, e.screenX, e.screenY, e.clientX, e.clientY, false, false, false, false, 0, null);
ele.dispatchEvent(evt);
}
}
//阻止事件默认行为并停止事件冒泡
function stop_event(event, isPreventDefault, isStopPropagation) {
var e = fix_event(event);
//阻止事件默认行为
if (isPreventDefault !== false) {
if (e.preventDefault) e.preventDefault();
else e.returnValue = false;
}
//停止事件冒泡
if (isStopPropagation !== false) {
if (e.stopPropagation) e.stopPropagation();
else e.cancelBubble = true;
}
}
//---------------------- other ----------------------
var RE_HTTP = /^https?:\/\//i;
//是否http路径(以 http:// 或 https:// 开头)
function isHttpURL(url) {
return RE_HTTP.test(url);
}
//判断指定路径与当前页面是否同域(包括协议检测 eg:http与https不同域)
function isSameHost(url) {
if (!isHttpURL(url)) return true;
var start = RegExp.lastMatch.length,
end = url.indexOf("/", start),
host = url.slice(0, end != -1 ? end : undefined);
return host.toLowerCase() == (location.protocol + "//" + location.host).toLowerCase();
}
//按照进制解析数字的层级 eg:时间转化 -> parseLevel(86400,[60,60,24]) => { value=1, level=3 }
//steps:步进,可以是固定的数字(eg:1024),也可以是具有层次关系的数组(eg:[60,60,24])
//limit:限制解析的层级,正整数,默认为100
function parseLevel(size, steps, limit) {
size = +size;
steps = steps || 1024;
var level = 0,
isNum = typeof steps == "number",
stepNow = 1,
count = isUInt(limit) ? limit : (isNum ? 100 : steps.length);
while (size >= stepNow && level < count) {
stepNow *= (isNum ? steps : steps[level]);
level++;
}
if (level && size < stepNow) {
stepNow /= (isNum ? steps : steps.last());
level--;
}
return { value: level ? size / stepNow : size, level: level };
}
var UNITS_FILE_SIZE = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
//格式化数字输出,将数字转为合适的单位输出,默认按照1024层级转为文件单位输出
function formatSize(size, ops) {
ops = ops === true ? { all: true } : ops || {};
if (isNaN(size) || size == undefined || size < 0) {
var error = ops.error || "--";
return ops.all ? { text: error } : error;
}
var pl = parseLevel(size, ops.steps, ops.limit),
value = pl.value,
text = value.toFixed(def(ops.digit, 2));
if (ops.trim !== false && text.lastIndexOf(".") != -1) text = text.replace(/\.?0+$/, "");
pl.text = text + (ops.join || "") + (ops.units || UNITS_FILE_SIZE)[pl.level + (ops.start || 0)];
return ops.all ? pl : pl.text;
}
//---------------------- export ----------------------
var Q = {
def: def,
isFunc: isFunc,
isUInt: isUInt,
fire: fire,
extend: extend,
ie: browser_ie,
setOpacity: setOpacity,
getOffset: getOffset,
walk: walk,
getPrev: getPrev,
getNext: getNext,
getFirst: getFirst,
getLast: getLast,
getChilds: getChilds,
createEle: createEle,
parseHTML: parseHTML,
isHttpURL: isHttpURL,
isSameHost: isSameHost,
parseLevel: parseLevel,
formatSize: formatSize
};
if (browser_ie) Q["ie" + (browser_ie < 6 ? 6 : browser_ie)] = true;
Q.event = {
fix: fix_event,
stop: stop_event,
trigger: trigger_event,
add: add_event
};
window.Q = Q;
})(window);
/*
* Q.Uploader.js 文件上传管理器 1.0
* https://github.com/devin87/web-uploader
* author:devin87@qq.com
* update:2022/01/07 14:48
*/
(function (window, undefined) {
"use strict";
var def = Q.def,
fire = Q.fire,
extend = Q.extend,
getFirst = Q.getFirst,
getLast = Q.getLast,
parseJSON = JSON.parse,
createEle = Q.createEle,
parseHTML = Q.parseHTML,
setOpacity = Q.setOpacity,
getOffset = Q.getOffset || Q.offset,
md5File = Q.md5File,
E = Q.event,
addEvent = E.add,
triggerEvent = E.trigger,
stopEvent = E.stop;
//Object.forEach
//Date.now
//-------------------------------- Uploader --------------------------------
var support_html5_upload = false, //是否支持html5(ajax)方式上传
support_multiple_select = false, //是否支持文件多选
support_file_click_trigger = false, //上传控件是否支持click触发文件选择 eg: input.click() => ie9及以下不支持
UPLOADER_GUID = 0, //文件上传管理器唯一标示,多用于同一个页面存在多个管理器的情况
UPLOAD_TASK_GUID = 0, //上传任务唯一标示
UPLOAD_HTML4_ZINDEX = 0; //防止多个上传管理器的触发按钮位置重复引起的问题
//上传状态
var UPLOAD_STATE_READY = 0, //任务已添加,准备上传
UPLOAD_STATE_PROCESSING = 1, //任务上传中
UPLOAD_STATE_COMPLETE = 2, //任务上传完成
UPLOAD_STATE_SKIP = -1, //任务已跳过(不会上传)
UPLOAD_STATE_CANCEL = -2, //任务已取消
UPLOAD_STATE_ERROR = -3; //任务已失败
var global_settings = {};
//Uploader全局设置
function setup(ops) {
extend(global_settings, ops, true);
}
//获取上传状态说明
function get_upload_status_text(state) {
var LANG = Uploader.Lang;
switch (state) {
case UPLOAD_STATE_READY: return LANG.status_ready;
case UPLOAD_STATE_PROCESSING: return LANG.status_processing;
case UPLOAD_STATE_COMPLETE: return LANG.status_complete;
case UPLOAD_STATE_SKIP: return LANG.status_skip;
case UPLOAD_STATE_CANCEL: return LANG.status_cancel;
case UPLOAD_STATE_ERROR: return LANG.status_error;
}
return state;
}
//上传探测
function detect() {
var XHR = window.XMLHttpRequest;
if (XHR && new XHR().upload && window.FormData) support_html5_upload = true;
var input = document.createElement("input");
input.type = "file";
support_multiple_select = !!input.files;
support_file_click_trigger = support_html5_upload;
}
//截取字符串
function get_last_find(str, find) {
var index = str.lastIndexOf(find);
return index != -1 ? str.slice(index) : "";
}
//将逗号分隔的字符串转为键值对
function split_to_map(str) {
if (!str) return;
var list = str.split(","), map = {};
for (var i = 0, len = list.length; i < len; i++) {
map[list[i]] = true;
}
return map;
}
//iframe load 事件
//注意:低版本 ie 支持 iframe 的 onload 事件,不过是隐形的(iframe.onload 方式绑定的将不会触发),需要通过 attachEvent 来注册
function bind_iframe_load(iframe, fn) {
if (iframe.attachEvent) iframe.attachEvent("onload", fn);
else iframe.addEventListener("load", fn, false);
}
//计算上传速度
function set_task_speed(task, total, loaded) {
if (!total || total <= 0) return;
var nowTime = Date.now(), tick;
//上传完毕,计算平均速度(Byte/s)
if (loaded >= total) {
tick = nowTime - task.startTime;
if (tick) task.avgSpeed = Math.min(Math.round(total * 1000 / tick), total);
else if (!task.speed) task.avgSpeed = task.speed = total;
task.time = tick || 0;
task.endTime = nowTime;
return;
}
//即时速度(Byte/s)
tick = nowTime - task.lastTime;
if (tick < 200) return;
var lastLoaded = task.lastLoaded || 0;
task.speed = Math.min(Math.round((loaded - lastLoaded) * 1000 / tick), task.total);
task.lastTime = nowTime;
task.lastLoaded = loaded;
}
/*
文件上传管理器,调用示例
new Uploader({
//--------------- 必填 ---------------
url: "", //上传路径
target: element, //上传按钮,可为数组
view: element, //上传任务视图(需加载UI接口默认实现)
//--------------- 可选 ---------------
html5: true, //是否启用html5上传,若浏览器不支持,则自动禁用
multiple: true, //选择文件时是否允许多选,若浏览器不支持,则自动禁用(仅html5模式有效)
clickTrigger:true, //是否启用click触发文件选择 eg: input.click() => ie9及以下不支持
auto: true, //添加任务后是否立即上传
data: {}, //上传文件的同时可以指定其它参数,该参数将以POST的方式提交到服务器
workerThread: 1, //同时允许上传的任务数(仅html5模式有效)
upName: "upfile", //上传参数名称,若后台需要根据name来获取上传数据,可配置此项
accept: "", //指定浏览器接受的文件类型 eg:image/*,video/*
isDir: false, //是否是文件夹上传(仅Webkit内核浏览器和新版火狐有效)
allows: "", //允许上传的文件类型(扩展名),多个之间用逗号隔开
disallows: "", //禁止上传的文件类型(扩展名)
maxSize: 2*1024*1024, //允许上传的最大文件大小,字节,为0表示不限(仅对支持的浏览器生效,eg: IE10+、Firefox、Chrome)
isSlice: false, //是否启用分片上传,若为true,则isQueryState和isMd5默认为true
chunkSize: 2 * 1024 * 1024, //默认分片大小为2MB
isQueryState:false, //是否查询文件状态(for 秒传或续传)
isMd5: false, //是否计算上传文件md5值
isUploadAfterHash:true, //是否在Hash计算完毕后再上传
sliceRetryCount:2, //分片上传失败重试次数
urlForQuery: "", //秒传或分片上传查询接口
container:element, //一般无需指定
getPos:function, //一般无需指定
//上传回调事件(function)
on: {
init, //上传管理器初始化完毕后触发
select, //点击上传按钮准备选择上传文件之前触发,返回false可禁止选择文件
add[Async], //添加任务之前触发,返回false将跳过该任务
query[Async], //秒传或断点续传获取到查询结果后触发,返回-1表示秒传成功,返回大于或等于0的数字表示上传起始点
upload[Async], //上传任务之前触发,返回false将跳过该任务
send[Async], //发送数据之前触发,返回false将跳过该任务
cancel, //取消上传任务后触发
remove, //移除上传任务后触发
progress, //上传进度发生变化后触发(仅html5模式有效)
complete //上传完成后触发
},
//UI接口(function),若指定了以下方法,将忽略默认实现
UI:{
init, //执行初始化操作
draw, //添加任务后绘制任务界面
update, //更新任务界面
over //任务上传完成
}
});
*/
function Uploader(settings) {
var self = this,
ops = settings || {};
self.guid = ops.guid || "uploader" + (++UPLOADER_GUID);
self.list = [];
self.map = {};
self.index = 0;
self.started = false;
self.set(ops)._init();
}
Uploader.prototype = {
//修复constructor指向
constructor: Uploader,
set: function (settings) {
var self = this,
ops = extend(settings, self.ops);
self.url = ops.url; //上传路径
self.dataType = ops.dataType || "json"; //返回值类型
self.data = ops.data; //上传参数
//上传按钮
self.targets = ops.target || [];
if (!self.targets.forEach) self.targets = [self.targets];
self.target = self.targets[0]; //当前上传按钮
//是否以html5(ajax)方式上传
self.html5 = support_html5_upload && !!def(ops.html5, true);
//是否允许多选(仅在启用了html5的情形下生效)
//在html4模式下,input是一个整体,若启用多选,将无法针对单一的文件进行操作(eg:根据扩展名筛选、取消、删除操作等)
//若无需对文件进行操作,可通过 uploader.multiple = true 强制启用多选(不推荐)
self.multiple = support_multiple_select && self.html5 && !!def(ops.multiple, true);
//是否启用click触发文件选择 eg: input.click() => IE9及以下不支持
self.clickTrigger = support_file_click_trigger && !!def(ops.clickTrigger, true);
//允许同时上传的数量(html5有效)
//由于设计原因,html4仅能同时上传1个任务,请不要更改
self.workerThread = self.html5 ? ops.workerThread || 1 : 1;
//空闲的线程数量
self.workerIdle = self.workerThread;
//是否在添加任务后自动开始
self.auto = ops.auto !== false;
//input元素的name属性
self.upName = ops.upName || "upfile";
//input元素的accept属性,用来指定浏览器接受的文件类型 eg:image/*,video/*
//注1:IE9及以下不支持accept属性
//注2:某些手机浏览器不支持扩展名的 accept 值,弹出的文件选择框没有文件选择项
self.accept = ops.accept || ((navigator.platform || '').indexOf('Win') == 0 ? ops.allows : "*/*");
//是否是文件夹上传,仅Webkit内核浏览器和新版火狐有效
self.isDir = ops.isDir;
//允许上传的文件类型(扩展名),多个之间用逗号隔开 eg:.jpg,.png
self.allows = split_to_map(ops.allows);
//禁止上传的文件类型(扩展名),多个之间用逗号隔开
self.disallows = split_to_map(ops.disallows);
//允许上传的最大文件大小,字节,为0表示不限(仅对支持的浏览器生效,eg: IE10+、Firefox、Chrome)
self.maxSize = +ops.maxSize || 0;
self.isSlice = !!ops.isSlice; //是否启用分片上传
self.chunkSize = +ops.chunkSize || 2 * 1024 * 1024; //分片上传大小
self.isQueryState = !!def(ops.isQueryState, self.isSlice); //是否查询文件状态(for 秒传或续传)
self.isMd5 = !!def(ops.isMd5, self.isSlice); //是否计算上传文件md5值
self.isUploadAfterHash = ops.isUploadAfterHash !== false; //是否在Hash计算完毕后再上传
self.sliceRetryCount = ops.sliceRetryCount == undefined ? 2 : +ops.sliceRetryCount || 0; //分片上传失败重试次数
self.urlForQuery = ops.urlForQuery || self.url; //秒传或分片上传查询接口
//ie9及以下不支持click触发(即使能弹出文件选择框,也无法获取文件数据,报拒绝访问错误)
//若上传按钮位置不确定(比如在滚动区域内),则无法触发文件选择
//设置原则:getPos需返回上传按钮距container的坐标
self.container = ops.container || document.body;
//函数,获取上传按钮距container的坐标,返回格式 eg:{ left: 100, top: 100 }
if (ops.getPos) self.getPos = ops.getPos;
//UI接口,此处将覆盖 prototype 实现
var UI = ops.UI || {};
if (UI.init) self.init = UI.init; //执行初始化操作
if (UI.draw) self.draw = UI.draw; //添加任务后绘制任务界面
if (UI.update) self.update = UI.update; //更新任务界面
if (UI.over) self.over = UI.over; //任务上传完成
//上传回调事件
self.fns = ops.on || {};
//上传选项
self.ops = ops;
if (self.accept && !self.clickTrigger) self.resetInput();
return self;
},
//初始化上传管理器
_init: function () {
var self = this;
if (self._inited) return;
self._inited = true;
var guid = self.guid,
container = self.container;
var boxInput = createEle("div", "upload-input " + guid);
container.appendChild(boxInput);
self.boxInput = boxInput;
//构造html4上传所需的iframe和form
if (!self.html5) {
var iframe_name = "upload_iframe_" + guid;
var html =
'<iframe class="u-iframe" name="' + iframe_name + '"></iframe>' +
'<form class="u-form" action="" method="post" enctype="multipart/form-data" target="' + iframe_name + '"></form>';
var boxHtml4 = createEle("div", "upload-html4 " + guid, html);
container.appendChild(boxHtml4);
var iframe = getFirst(boxHtml4),
form = getLast(boxHtml4);
self.iframe = iframe;
self.form = form;
//html4上传完成回调
bind_iframe_load(iframe, function () {
if (self.workerIdle != 0) return;
var text;
try {
text = iframe.contentWindow.document.body.innerHTML;
} catch (e) { }
self.complete(undefined, UPLOAD_STATE_COMPLETE, text);
});
}
self.targets.forEach(function (target) {
if (self.clickTrigger) {
addEvent(target, "click", function (e) {
if (self.fire("select", e) === false) return;
self.resetInput();
//注意:ie9及以下可以弹出文件选择框,但获取不到选择数据,拒绝访问。
triggerEvent(self.inputFile, "click");
});
} else {
addEvent(target, "mouseover", function (e) {
self.target = this;
self.updatePos();
});
}
});
//html4点击事件
if (!self.clickTrigger) {
addEvent(boxInput, "click", function (e) {
if (self.fire("select", e) === false) stopEvent(e);
});
setOpacity(boxInput, 0);
self.resetInput();
}
return self.run("init", undefined, "init");
},
//重置上传控件
resetInput: function () {
var self = this,
boxInput = self.boxInput;
if (!boxInput) return self;
boxInput.innerHTML = '<input type="file" name="' + self.upName + '"' + (self.accept ? 'accept="' + self.accept + '"' : '') + (self.isDir ? 'webkitdirectory=""' : '') + ' style="' + (self.clickTrigger ? 'visibility: hidden;' : 'font-size:100px;') + '"' + (self.multiple ? ' multiple="multiple"' : '') + '>';
var inputFile = getFirst(boxInput);
//文件选择事件
addEvent(inputFile, "change", function (e) {
self.add(this);
//html4 重置上传控件
if (!self.html5) self.resetInput();
});
self.inputFile = inputFile;
return self.updatePos();
},
//更新上传按钮坐标(for ie)
updatePos: function (has_more_uploader) {
var self = this;
if (self.clickTrigger) return self;
var getPos = self.getPos || getOffset,
boxInput = self.boxInput,
inputFile = getFirst(boxInput),
target = self.target,
inputWidth = target.offsetWidth,
inputHeight = target.offsetHeight,
pos = inputWidth == 0 ? { left: -10000, top: -10000 } : getPos(target);
boxInput.style.width = inputFile.style.width = inputWidth + "px";
boxInput.style.height = inputFile.style.height = inputHeight + "px";
boxInput.style.left = pos.left + "px";
boxInput.style.top = pos.top + "px";
//多用于选项卡切换中上传按钮位置重复的情况
if (has_more_uploader) boxInput.style.zIndex = ++UPLOAD_HTML4_ZINDEX;
return self;
},
//触发回调方法,优先触发异步回调(以Async结尾)
fire: function (action, arg, callback) {
var self = this,
fns = self.fns || {},
globalFns = global_settings.on || {},
result;
if (!callback) {
result = fire(fns[action], self, arg);
var result2 = fire(globalFns[action], self, arg);
return result != undefined ? result : result2;
}
var next2 = function (result2) {
callback(result != undefined ? result : result2);
};
var next = function (result1) {
result = result1;
var globalAsyncFun = globalFns[action + "Async"];
if (globalAsyncFun) return fire(globalAsyncFun, self, arg, next2);
next2(fire(globalFns[action], self, arg));
};
var asyncFun = fns[action + "Async"];
if (asyncFun) return fire(asyncFun, self, arg, next);
next(fire(fns[action], self, arg));
},
//运行内部方法或扩展方法(如果存在)
//action_event: 若存在,则触发ops上定义的回调方法
run: function (action, arg, action_event) {
var fn = this[action];
if (fn) fire(fn, this, arg);
if (action_event) this.fire(action_event, arg)
return this;
},
//添加一个上传任务
addTask: function (input, file) {
if (!input && !file) return;
var name, size;
if (file) {
name = file.webkitRelativePath || file.name || file.fileName;
size = file.size === 0 ? 0 : file.size || file.fileSize;
} else {
name = get_last_find(input.value, "\\").slice(1) || input.value;
size = -1;
}
var self = this,
ext = get_last_find(name, ".").toLowerCase(),
limit_type;
if ((self.disallows && self.disallows[ext]) || (self.allows && !self.allows[ext])) limit_type = "ext";
else if (size != -1 && self.maxSize && size > self.maxSize) limit_type = "size";
var task = {
id: ++UPLOAD_TASK_GUID,
name: name,
ext: ext,
size: size,
input: input,
file: file,
state: limit_type ? UPLOAD_STATE_SKIP : UPLOAD_STATE_READY
};
if (limit_type) {
task.limited = limit_type;
task.disabled = true;
}
self.fire("add", task, function (result) {
if (result === false || task.disabled || task.limited) return;
task.index = self.list.length;
self.list.push(task);
self.map[task.id] = task;
self.run("draw", task, "draw");
if (self.auto) self.start();
});
return task;
},
//添加上传任务,自动判断input(是否多选)或file
add: function (input_or_file) {
var self = this;
if (input_or_file.tagName == "INPUT") {
var files = input_or_file.files;
if (files) {
for (var i = 0, len = files.length; i < len; i++) {
self.addTask(input_or_file, files[i]);
}
} else {
self.addTask(input_or_file);
}
} else {
self.addTask(undefined, input_or_file);
}
},
//批量添加上传任务
addList: function (list) {
for (var i = 0, len = list.length; i < len; i++) {
this.add(list[i]);
}
},
//获取指定任务
get: function (taskId) {
if (taskId != undefined) return this.map[taskId];
},
//取消上传任务
//onlyCancel: 若为true,则仅取消上传而不触发任务完成事件
cancel: function (taskId, onlyCancel) {
var self = this,
task = self.get(taskId);
if (!task) return;
var state = task.state;
//若任务已完成,直接返回
if (state != UPLOAD_STATE_READY && state != UPLOAD_STATE_PROCESSING) return self;
if (state == UPLOAD_STATE_PROCESSING) {
//html5
var xhr = task.xhr;
if (xhr) {
xhr.abort();
//无需调用complete,html5 有自己的处理,此处直接返回
return self;
}
//html4
self.iframe.contentWindow.location = "about:blank";
}
return onlyCancel ? self : self.complete(task, UPLOAD_STATE_CANCEL);
},
//移除任务
remove: function (taskId) {
var task = this.get(taskId);
if (!task) return;
if (task.state == UPLOAD_STATE_PROCESSING) this.cancel(taskId);
//this.list.splice(task.index, 1);
//this.map[task.id] = undefined;
//从数组中移除任务时,由于任务是根据index获取,若不处理index,将导致上传错乱甚至不能上传
//此处重置上传索引,上传时会自动修正为正确的索引(程序会跳过已处理过的任务)
//this.index = 0;
//添加移除标记(用户可以自行操作,更灵活)
task.deleted = true;