Skip to content

Commit d6daee3

Browse files
committed
[feature] Upload base64 or dataURL use QiNiu.
1 parent 79d6cb1 commit d6daee3

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

lib/file.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ 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+
4872
// A list of file extensions to mime types as found here:
4973
// http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-
5074
// mime-type-of-a-file-based-on-the-file-signature
@@ -330,6 +354,7 @@ module.exports = function(AV) {
330354
this._metaData.size = data.length;
331355
} else if (data && data.base64) {
332356
this._source = AV.Promise.as(data.base64, guessedType);
357+
this._isBase64 = true;
333358
} else if (data && data.blob) {
334359
this._source = AV.Promise.as(data.blob, guessedType);
335360
this._isBlob = true;
@@ -557,11 +582,15 @@ module.exports = function(AV) {
557582
var self = this;
558583
if (!self._previousSave) {
559584
if(self._source) {
560-
if (this._isBlob || this._isFileObject) {
585+
if (self._isBlob || self._isFileObject || self._isBase64) {
561586
// TODO: remove this code and use qiniu SDK
562587
var dataFormat;
563588
self._previousSave = self._source.then(function(data, type) {
564-
dataFormat = data;
589+
if (self._isBase64) {
590+
dataFormat = dataURItoBlob(data, type);
591+
} else {
592+
dataFormat = data;
593+
}
565594
return self._qiniuToken(type);
566595
}).then(function(response) {
567596
self._url = response.url;

0 commit comments

Comments
 (0)