dataURLtoBlob (dataurl) {
var arr = dataurl.split(',')
// var mime = arr[0].match(/:(.*?);/)[1]
var bstr = atob(arr[1])
var n = bstr.length
var u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
console.log(u8arr)
return u8arr
// return new Blob([u8arr], {
// type: mime
// })
},
stringToByte (base64) {
var arr = base64.split(',')
var str = window.atob(arr[1])
var bytes = []
var len, c
len = str.length
for (var i = 0; i < len; i++) {
c = str.charCodeAt(i)
if (c >= 0x010000 && c <= 0x10FFFF) {
bytes.push(((c >> 18) & 0x07) | 0xF0)
bytes.push(((c >> 12) & 0x3F) | 0x80)
bytes.push(((c >> 6) & 0x3F) | 0x80)
bytes.push((c & 0x3F) | 0x80)
} else if (c >= 0x000800 && c <= 0x00FFFF) {
bytes.push(((c >> 12) & 0x0F) | 0xE0)
bytes.push(((c >> 6) & 0x3F) | 0x80)
bytes.push((c & 0x3F) | 0x80)
} else if (c >= 0x000080 && c <= 0x0007FF) {
bytes.push(((c >> 6) & 0x1F) | 0xC0)
bytes.push((c & 0x3F) | 0x80)
} else {
bytes.push(c & 0xFF)
}
}
return bytes
},
base64ToArrayBuffer (base64) {
var arr = base64.split(',')
var binaryString = window.atob(arr[1])
var len = binaryString.length
var bytes = new Uint8Array(len)
for (var i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i)
}
console.log(bytes.buffer)
return bytes.buffer
}
base64转为byte,blob等方法
最新推荐文章于 2024-07-31 16:45:57 发布