Base64图片转Blob对象

时间:2023-03-09 16:47:35
Base64图片转Blob对象

//将Base64图片转成Blob对象

//@args: base64Url:编码字符串,contentType:类型。

function base64UrltoBlob(base64Url, contentType) {

var data = base64Url.slice(base64Url.search('base64') + 7);
var sliceSize = 1024;
var byteCharacters = base64.toByteArray(data);
var bytesLength = byteCharacters.length;
var slicesCount = Math.ceil(bytesLength / sliceSize);
var byteArrays = new Array(slicesCount);

for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
var begin = sliceIndex * sliceSize;
var end = Math.min(begin + sliceSize, bytesLength);
var bytes = new Array(end - begin);
for (var offset = begin, i = 0; offset < end; ++i, ++offset) {
bytes[i] = byteCharacters[offset];
}
byteArrays[sliceIndex] = new Uint8Array(bytes);
}
return new Blob(byteArrays, { type: contentType });
}