Skip to content

Commit 6c853fb

Browse files
committed
[feature] Upload base64 support nodejs, use browserify .
1 parent 1530e72 commit 6c853fb

File tree

5 files changed

+39
-29
lines changed

5 files changed

+39
-29
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
var dataURItoBlob = function(dataURI, type) {
4+
var byteString;
5+
6+
// 传入的 base64,不是 dataURL
7+
if (dataURI.indexOf('base64') < 0) {
8+
byteString = atob(dataURI);
9+
} else if (dataURI.split(',')[0].indexOf('base64') >= 0) {
10+
byteString = atob(dataURI.split(',')[1]);
11+
} else {
12+
byteString = unescape(dataURI.split(',')[1]);
13+
}
14+
// separate out the mime component
15+
var mimeString = type || dataURI.split(',')[0].split(':')[1].split(';')[0];
16+
var ia = new Uint8Array(byteString.length);
17+
for (var i = 0; i < byteString.length; i ++) {
18+
ia[i] = byteString.charCodeAt(i);
19+
}
20+
return new Blob([ia], {type:mimeString});
21+
};
22+
23+
module.exports = dataURItoBlob;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
var parseBase64 = function(base64) {
4+
// 兼容 dataURL
5+
if (base64.split(',')[0] && base64.split(',')[0].indexOf('base64') >= 0) {
6+
base64 = base64.split(',')[1];
7+
}
8+
return new Buffer(base64, 'base64').toString('utf8');
9+
};
10+
11+
module.exports = parseBase64;

lib/browserify-wrapper/upload.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
//Use qiniu sdk to upload files to qiniu.
34
var qiniu = require('qiniu');
45
var Promise = require('../promise');
56

@@ -8,8 +9,6 @@ module.exports =function upload(file) {
89
file._base64 = base64;
910
return file._qiniuToken(type);
1011
}).then(function(response) {
11-
//Use qiniu sdk to upload files to qiniu.
12-
var qiniu = require('qiniu');
1312
file._url = response.url;
1413
file._bucket = response.bucket;
1514
file.id = response.objectId;

lib/file.js

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ module.exports = function(AV) {
4545
return chunks.join("");
4646
};
4747

48-
49-
var dataURItoBlob = function(dataURI, type) {
50-
// convert base64/URLEncoded data component to raw binary data held in a string
51-
var byteString;
52-
53-
// 传入的 base64,不是 dataURL
54-
if (dataURI.indexOf('base64') < 0) {
55-
byteString = atob(dataURI);
56-
} else if (dataURI.split(',')[0].indexOf('base64') >= 0) {
57-
byteString = atob(dataURI.split(',')[1]);
58-
} else {
59-
byteString = unescape(dataURI.split(',')[1]);
60-
}
61-
// separate out the mime component
62-
var mimeString = type || dataURI.split(',')[0].split(':')[1].split(';')[0];
63-
64-
// write the bytes of the string to a typed array
65-
var ia = new Uint8Array(byteString.length);
66-
for (var i = 0; i < byteString.length; i ++) {
67-
ia[i] = byteString.charCodeAt(i);
68-
}
69-
return new Blob([ia], {type:mimeString});
70-
};
71-
7248
// A list of file extensions to mime types as found here:
7349
// http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-
7450
// mime-type-of-a-file-based-on-the-file-signature
@@ -353,8 +329,8 @@ module.exports = function(AV) {
353329
this._source = AV.Promise.as(encodeBase64(data), guessedType);
354330
this._metaData.size = data.length;
355331
} else if (data && data.base64) {
356-
// 也可以是 dataURL,内部已做兼容
357-
var dataBase64 = dataURItoBlob(data.base64, guessedType);
332+
var parseBase64 = require('./browserify-wrapper/parseBase64');
333+
var dataBase64 = parseBase64(data.base64, guessedType);
358334
this._source = AV.Promise.as(dataBase64, guessedType);
359335
} else if (data && data.blob) {
360336
this._source = AV.Promise.as(data.blob, guessedType);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"browser": {
4040
"./lib/browserify-wrapper/upload.js": "./lib/browserify-wrapper/upload-browser.js",
4141
"xmlhttprequest": "./lib/browserify-wrapper/xmlhttprequest-browser.js",
42-
"localStorage": "./lib/browserify-wrapper/localstorage-browser.js"
42+
"localStorage": "./lib/browserify-wrapper/localstorage-browser.js",
43+
"parseBase64": "./lib/browserify-wrapper/parseBase64-browser.js"
4344
}
4445
}

0 commit comments

Comments
 (0)