项目中经常会上传图片,上传完以后要回显,这里回显使用的是base64地址回显,特此记录一下img文件转base64。
写个demo记录一下:
<input type="file" name="" id="" @change="changeFile" />
<img :src="imgsrc" alt="" srcset="" />
changeFile(files) {
let file = files.target.files[0];
this.readFile(file);
},
readFile(file) {
let than = this;
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
console.log(this.result);
than.imgsrc = reader.result;
};
},