上传图片时候有时候要进行裁剪,裁剪完成后的格式是base64,后端一般要formdata格式,我们就要先转file流格式然后在formdata格式
转file
base64ToFile(base64, fileName) {
const arr = base64.split(",");
const type = arr[0].match(/:(.*?);/)[1];
const bstr = atob(arr[1]);
let n = bstr.length;
const u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], fileName, { type });
}
将file转formdata格式
let fd = new formData()
fd.append("shopLogoImg", file)