Created
December 25, 2013 11:19
-
-
Save puterjam/8122316 to your computer and use it in GitHub Desktop.
convertDataURIToBlob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* DataURI转换成Blob格式 | |
* @param {string} dataURI base64 的 data uri | |
* @param {string} mimetype 需要输出的blob的mimetype格式 | |
* @return {Blob} 大二进制数据块 | |
*/ | |
var convertDataURIToBlob = function convertDataURIToBlob(dataURI, mimetype) { | |
//分割DataURI成 base64格式; | |
var BASE64_HEADER = ';base64,'; | |
var base64Index = dataURI.indexOf(BASE64_HEADER) + BASE64_HEADER.length; | |
var base64 = dataURI.substring(base64Index); | |
//将已经被base64编码过的数据进行解码 | |
var raw = window.atob(base64); // more https://developer.mozilla.org/zh-CN/docs/DOM/window.atob | |
var rawLength = raw.length; | |
//编码成 8 位无符号整数值的类型化数组 | |
var uInt8Array = new Uint8Array(rawLength); | |
for (var i = 0; i < rawLength; ++i) { | |
uInt8Array[i] = raw.charCodeAt(i); | |
} | |
//返回blob对象 | |
return new Blob([uInt8Array], { | |
type: mimetype | |
}); // more https://developer.mozilla.org/zh-CN/docs/DOM/Blob | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment