-
Notifications
You must be signed in to change notification settings - Fork 16
/
MobileUpload.JS
118 lines (113 loc) · 3.78 KB
/
MobileUpload.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
module.exports = function (options) {
var _defaultSetting = {
loading: '.loading',
url: '', //接收数据的api接口地址
maxFileSize: 10 * 1024 * 1024,
format: 'image',
isCompress: true,
compressNum: 0.6, // 0~1 设置为1可能最终结果比未压缩还大,请慎用1.
beforeUpload: function () {
},
uploadStart: function () {
},
afterUpload: function () {
},
uploadProgress: function (v) {
},
uploadError: function () {
},
showThumbnail: function () {
}
},
opt = {};
function startUpload(el) {
var files = el.files,
len = files.length;
for (var i = len - 1; i >= 0; i--) {
(function (file) {
if (file.size > opt.maxFileSize) {
console.log('您上传的' + file.name + ',图片尺寸过大,最大限制为10M');
return false;
}
//有些安卓手机无法获取文件类型
if (new RegExp(opt.format, 'i').test(file.type) || !file.type) {
if (opt.beforeUpload() === false) {
return false;
}
readFile(file);
} else {
console.log("您上传的文件不符合上传的要求");
}
}(files[i]));
}
}
function readFile(file) {
var reader = new FileReader();
reader.onload = function () {
file = this.result;
opt.uploadStart && opt.uploadStart(file);
upLoadFile(file);
this.result = null;
reader.onload = null;
reader = null;
};
reader.onprogress = function (e) {
if (e.lengthComputable) {
var percentLoaded = Math.round((e.loaded / e.total) * 100);
opt.uploadProgress && opt.uploadProgress(percentLoaded);
}
};
reader.onabort = function () {
opt.uploadError && opt.uploadError();
}
if (opt.format = 'image') {
reader.readAsDataURL(file);
} else {
reader.readAsBinaryString(file);
opt.isCompress = false;
}
}
function upLoadFile(file) {
if (opt.format != 'image') {
ajaxUploadFile(file);
} else {
var _canvas = $('<canvas style="display: none"></canvas>')[0];
var img = new Image();
img.onload = function () {
if (opt.isCompress) {
_canvas.width = img.naturalWidth;
_canvas.height = img.naturalHeight;
var context = _canvas.getContext('2d');
context.drawImage(img, 0, 0);
ajaxUploadFile(_canvas.toDataURL('image/jpeg', opt.compressNum));
} else {
ajaxUploadFile(file);
}
};
img.src = file;
}
}
function ajaxUploadFile(file) {
$(opt.loading).show();
opt.showThumbnail && opt.showThumbnail(file);
$.ajax({
url: opt.url,
type: 'post',
data: {
file: file
},
success: function (d) {
$(opt.loading).hide();
opt.afterUpload && opt.afterUpload(file, d);
},
error: function (d) {
$(opt.loading).hide();
opt.uploadError && opt.uploadError(d);
}
});
}
opt = $.extend(true, _defaultSetting, options);
$(this).on('change', function () {
startUpload(this);
});
}