浏览器上传图片
async uploadImage(e) {
let _this = this;
//上传图片并预览
let file = e.target.files[0]; //获取第一个文件
let fileSize = file.size / 1024 > 805;
if (fileSize) {
this.$message.error("上传图片大小不能超过 805kb,请重新选择图片");
return;
}
console.log(file.size / 1024, "图片大小");
this.getBase64(file).then((res) => {
let imgF = res.split(",");
_this.uploadImg(imgF[1]);
console.log(imgF, "lll");
});
// this.base64Img=base64Img
// this.uploadImg(base64Img)
// console.log(file,'图片',base64Img)
// //
// let img = new FileReader();
// img.readAsDataURL(file);
// console.log("img:",img)
// img.onload = ({ target }) => {
// this.imgSrc = target.result; //将img转化为二进制数据
// console.log("target:",target)
// this.uploadImg(target.result)
// };
//
},
//blob转base64
blobToBase64(blob) {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = (e) => {
resolve(e.target.result);
};
fileReader.readAsDataURL(blob);
fileReader.onerror = () => {
reject(new Error("文件流异常"));
};
});
},
勤学似春起之苗,不见其增,日有所长; 辍学如磨刀之石,不见其损,日所有亏!