element upload预览_vue element upload实现图片本地预览
vue使用element實(shí)現(xiàn)本地預(yù)覽,最主要的是將圖片路徑轉(zhuǎn)換為base64,供大家參考,具體內(nèi)容如下
HTML
class="avatar-uploader"
action="123" //這個(gè)路徑不重要,可以隨便寫
:show-file-list="false"
:on-success="handleAvatarSuccess"
:on-change="onchange"
:before-upload="beforeAvatarUpload">
js部分
export default {
data() {
return {
imageUrl: '',
};
},
methods: {
handleAvatarSuccess(res, file) {
this.imageUrl = URL.createObjectURL(file.raw);
},
beforeAvatarUpload(file) {
const isJPG = file.type === 'image/jpeg';
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error('上傳頭像圖片只能是 JPG 格式!');
}
if (!isLt2M) {
this.$message.error('上傳頭像圖片大小不能超過 2MB!');
}
return isJPG && isLt2M;
},
//當(dāng)上傳圖片后,調(diào)用onchange方法,獲取圖片本地路徑
onchange(file,fileList){
var _this = this;
var event = event || window.event;
var file = event.target.files[0];
var reader = new FileReader();
//轉(zhuǎn)base64
reader.onload = function(e) {
_this.imageUrl = e.target.result //將圖片路徑賦值給src
}
reader.readAsDataURL(file);
}
}
}
現(xiàn)在就可實(shí)現(xiàn)圖片本地預(yù)覽了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
本條技術(shù)文章來源于互聯(lián)網(wǎng),如果無意侵犯您的權(quán)益請(qǐng)點(diǎn)擊此處反饋版權(quán)投訴
本文系統(tǒng)來源:php中文網(wǎng)
總結(jié)
以上是生活随笔為你收集整理的element upload预览_vue element upload实现图片本地预览的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 监控 开源_强大的开源企业级
- 下一篇: arcgis vue 添加图层优化_行业