base64图片格式:
base64图片格式转化为文件流代码:
//data:base64图片格式字符串
//filename:文件名称
base64toFile(data, fileName) {
const dataArr = data.split(",");
const byteString = atob(dataArr[1]);
const options = {
type: "image/jpeg",
endings: "native"
};
const u8Arr = new Uint8Array(byteString.length);
for (let i = 0; i < byteString.length; i++) {
u8Arr[i] = byteString.charCodeAt(i);
}
return new File([u8Arr], fileName + ".jpg", options);//返回文件流
},
转换之后文件格式:
base64图片格式转换为文件流后,传送给后端
let str = this.base64toFile(imageStr,'file')//base64图片格式转文件流
let formData = new FormData();
formData.append("file", str);
//发送给后端
uploadImg(formData).then(response => {
this.imageList.push(response.data.url);
console.log(this.imageList);
}).catch(response => {
})