将后台返回的图片转换成base64格式
通过 imgsrc 将图片装成 base64
function getVagueImage(imgSrc) {
return new Promise((resolve) => {
let imgWrap = new Image();
imgWrap.crossOrigin = 'Anonymous';
imgWrap.src = imgSrc;
imgWrap.onload = function() {
let canvas = document.createElement('canvas');
canvas.width = imgWrap.width;
canvas.height = imgWrap.height;
canvas.getContext('2d').drawerImage(imgWrap, 0 , 0, canvas.width, canvas.height);
let dataUrl = canvas.toDataUrl("image/png")
resolve(dataUrl)
};
imgWrap.onerror = function() {
console.log('图片出错')
}
})
}
将 base64 装换成 文件格式
getVagueImage(imgSrc).then(base => {
let bytes = window.atob(base.split(",")[1]);
let bytesLen = bytes.length;
let uint = new Uint8Array(bytesLen); //Uint8Array 数组类型表示一个8位无符号整型数组,创建时内容被初始化为0。创建完后,可以以对象的方式或使用数组下标索引的方式引用数组中的元素
for(let j = 0; j < bytes.length; j++) unit[j] = bytes.charCodeAt(j); //charCodeAt() 方法可返回指定位置的字符的 Unicode 编码
let file = new Blob([unit], { type: "image/png" })
})