Skip to content

Commit 06542cb

Browse files
committed
upgrade to 2.1.0
上传图片新增在前端进行压缩处理。
1 parent 13f0502 commit 06542cb

File tree

2 files changed

+101
-6
lines changed

2 files changed

+101
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ __url__
4545

4646
上传接口地址,必选。
4747

48-
__headers__ (新增)
48+
__headers__ (v2.0.9 新增)
4949

5050
json对象,上传请求的头部信息,可选
5151

@@ -61,6 +61,10 @@ __connectionCount__
6161

6262
允许同时上传的文件数量,可选,默认值是3
6363

64+
__compress__(v2.1.0 新增)
65+
66+
上传图片在前端是否进行压缩,可选,默认为true
67+
6468
####方法
6569

6670
uploader实例会暴露一些公共方法:

lib/uploader.js

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ Uploader = (function(superClass) {
3333
headers: null,
3434
params: null,
3535
fileKey: 'upload_file',
36-
connectionCount: 3
36+
connectionCount: 3,
37+
compress: true
3738
};
3839

3940
Uploader.prototype._init = function() {
@@ -70,7 +71,8 @@ Uploader = (function(superClass) {
7071
})();
7172

7273
Uploader.prototype.upload = function(file, opts) {
73-
var f, i, key, len;
74+
var f, i, key, len, _this;
75+
_this = this;
7476
if (opts == null) {
7577
opts = {};
7678
}
@@ -102,8 +104,12 @@ Uploader = (function(superClass) {
102104
if (this.triggerHandler('beforeupload', [file]) === false) {
103105
return;
104106
}
105-
this.files.push(file);
106-
this._xhrUpload(file);
107+
108+
this.compressImageFile(file.obj ,function(blob_data){
109+
file.obj = blob_data;
110+
_this.files.push(file);
111+
_this._xhrUpload(file);
112+
});
107113
return this.uploading = true;
108114
};
109115

@@ -123,7 +129,8 @@ Uploader = (function(superClass) {
123129
name: name,
124130
size: (ref1 = fileObj.fileSize) != null ? ref1 : fileObj.size,
125131
ext: name ? name.split('.').pop().toLowerCase() : '',
126-
obj: fileObj
132+
obj: fileObj,
133+
compress: this.opts.compress
127134
};
128135
};
129136

@@ -234,6 +241,90 @@ Uploader = (function(superClass) {
234241
}
235242
};
236243

244+
Uploader.prototype.compressImageFile = function(fileObj, callback) {
245+
var fileReader, img;
246+
if (!$.isFunction(callback)) {
247+
return;
248+
}
249+
250+
if(!this.opts.compress){
251+
return callback(fileObj);
252+
}
253+
254+
img = new Image();
255+
256+
if (window.FileReader && FileReader.prototype.readAsDataURL && /^image/.test(fileObj.type)) {
257+
fileReader = new FileReader();
258+
var _this = this;
259+
fileReader.onload = function(e) {
260+
img.src = e.target.result;
261+
262+
//resize the image using canvas
263+
var canvas = document.createElement("canvas");
264+
var ctx = canvas.getContext("2d");
265+
ctx.drawImage(img, 0, 0);
266+
var MAX_WIDTH = 800;
267+
var MAX_HEIGHT = 800;
268+
var width = img.width;
269+
var height = img.height;
270+
271+
// less than the limited size of the returned directly
272+
if (width < MAX_WIDTH && height < MAX_HEIGHT) {
273+
return callback(fileObj);
274+
}
275+
276+
if (width > height) {
277+
if (width > MAX_WIDTH) {
278+
height *= MAX_WIDTH / width;
279+
width = MAX_WIDTH;
280+
}
281+
} else {
282+
if (height > MAX_HEIGHT) {
283+
width *= MAX_HEIGHT / height;
284+
height = MAX_HEIGHT;
285+
}
286+
}
287+
canvas.width = width;
288+
canvas.height = height;
289+
var ctx = canvas.getContext("2d");
290+
ctx.drawImage(img, 0, 0, width, height);
291+
292+
//change the dataUrl to blob data for uploading to server
293+
var dataURL = canvas.toDataURL('image/jpeg');
294+
var blob = _this.dataURItoBlob(dataURL);
295+
296+
return callback(blob);
297+
298+
};
299+
return fileReader.readAsDataURL(fileObj);
300+
} else {
301+
return callback(fileObj);
302+
}
303+
};
304+
305+
Uploader.prototype.dataURItoBlob = function(dataURI) {
306+
// convert base64/URLEncoded data component to raw binary data held in a string
307+
var byteString;
308+
if (dataURI.split(',')[0].indexOf('base64') >= 0)
309+
byteString = atob(dataURI.split(',')[1]);
310+
else
311+
byteString = unescape(dataURI.split(',')[1]);
312+
313+
// separate out the mime component
314+
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
315+
316+
// write the bytes of the string to a typed array
317+
var ia = new Uint8Array(byteString.length);
318+
for (var i = 0; i < byteString.length; i++) {
319+
ia[i] = byteString.charCodeAt(i);
320+
}
321+
322+
return new Blob([ia], {
323+
type: mimeString
324+
});
325+
326+
};
327+
237328
Uploader.prototype.destroy = function() {
238329
var file, i, len, ref;
239330
this.queue.length = 0;

0 commit comments

Comments
 (0)