Base64转Blob或者File
changeBase64ToFileOrBlob(base64String, type = "file") {
let arr = base64String.split(",");
let mime = arr[0].match(/:(.*?);/)[1]; // 此处得到的为文件类型
let bstr = atob(arr[1]); // 此处将base64解码
let n = bstr.length;
let u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
if (type === "blob") {
let blob = new Blob([u8arr], { type: mime });
return blob;
} else {
// 通过以下方式将以上变量生成文件对象,三个参数分别为文件内容、文件名、文件类型
let file = new File([u8arr], "filename", { type: mime });
return file;
}
},
ArrayBuffer转base64
function arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
Blob转Base64
export function blobToBase64 (blob) {
return new Promise((resolve, reject) => {
const fileReader = new FileReader()
fileReader.onload = (e) => {
resolve(e.target.result)
}
// readAsDataURL
fileReader.readAsDataURL(blob)
fileReader.onerror = () => {
reject(new Error('blobToBase64 error'))
}
})
}
网友评论