element 上传图片转base64


HTML

<el-upload
class="avatar-uploader"
action=""
:show-file-list="false"
:on-change="getFile"
:limit="1"
list-type="picture"
:auto-upload="false"
>
<img v-if="imageUrl" :src="imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>

JS

getFile (file, fileList) {
this.getBase64(file.raw).then(res => {
this.imageUrl = res
console.log(res)
})
},
getBase64 (file) {
return new Promise(function (resolve, reject) {
let reader = new FileReader()
let imgResult = ''
reader.readAsDataURL(file)
reader.onload = function () {
imgResult = reader.result
}
reader.onerror = function (error) {
reject(error)
}
reader.onloadend = function () {
resolve(imgResult)
}
})
}

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM