<template>
<div class="mine">
<input type="file" @change="upfileimg($event)" value="上传图片测试">
<img :src="myImg" >
</div>
</template>
<script>
export default {
data() {
return {
myImg: '',
}
},
methods: {
upfileimg(e) {
let img = e.target.files[0];
console.log(img);
if (img) {
let url = URL.createObjectURL(img);
let base64 = this.blobToDataURL(img, (base64Url) => {
console.log(base64Url);
this.myImg = base64Url
});
}
},
blobToDataURL(blob, cb) {
let reader = new FileReader();
reader.onload = function(evt) {
let base64 = evt.target.result;
cb(base64);
};
reader.readAsDataURL(blob);
},
},
}
</script>
|