封装函数如下:
const base64ToBlob = (base64String:any)=> {
const parts = base64String.split(';base64,');
const contentType = parts[0].split(':')[1];
const raw = window.atob(parts[1]);
const rawLength = raw.length;
const uInt8Array = new Uint8Array(rawLength);
for (let i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
return new Blob([uInt8Array], { type: contentType });
}
const imgUrl = canvas.toDataURL('image/png');
// 将 base64 编码的图片数据转换为 Blob 对象
const blob = base64ToBlob(imgUrl);
let fileDate = moment().format('YYYYMMDD')
let fileName = `${manualForm.tel}_${fileDate}.png`
// 创建 File 对象
const file = new File([blob], fileName, { type: 'image/png' });
// 创建 FormData 对象并添加文件
let formData = new FormData();
formData.append('file', file);
//start
let res: any = await post('/imgUpload',
formData
)