Commit bd8579dd authored by Vincent Bechu's avatar Vincent Bechu Committed by Vincent Bechu

[util] Add base64toBlob function

parent b345214e
...@@ -165,27 +165,32 @@ ...@@ -165,27 +165,32 @@
} }
util.stringify = stringify; util.stringify = stringify;
function base64toBlob(b64String, mimeString) {
// https://gist.github.com/davoclavo/4424731 var byteString = atob(b64String),
function dataURItoBlob(dataURI) {
if (dataURI === 'data:') {
return new Blob();
}
// convert base64 to raw binary data held in a string
var byteString = atob(dataURI.split(',')[1]),
// separate out the mime component
mimeString = dataURI.split(',')[0].split(':')[1],
// write the bytes of the string to an ArrayBuffer // write the bytes of the string to an ArrayBuffer
arrayBuffer = new ArrayBuffer(byteString.length), arrayBuffer = new ArrayBuffer(byteString.length),
_ia = new Uint8Array(arrayBuffer), _ia = new Uint8Array(arrayBuffer),
i; i;
mimeString = mimeString.slice(0, mimeString.length - ";base64".length);
for (i = 0; i < byteString.length; i += 1) { for (i = 0; i < byteString.length; i += 1) {
_ia[i] = byteString.charCodeAt(i); _ia[i] = byteString.charCodeAt(i);
} }
return new Blob([arrayBuffer], {type: mimeString}); return new Blob([arrayBuffer], {type: mimeString});
} }
util.base64toBlob = base64toBlob;
// https://gist.github.com/davoclavo/4424731
function dataURItoBlob(dataURI) {
if (dataURI === 'data:') {
return new Blob();
}
// convert base64 to raw binary data held in a string
var mimeString = dataURI.split(',')[0].split(':')[1];
mimeString = mimeString.slice(0, mimeString.length - ";base64".length);
return base64toBlob(dataURI.split(',')[1], mimeString);
}
util.dataURItoBlob = dataURItoBlob; util.dataURItoBlob = dataURItoBlob;
// tools // tools
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment