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)
}
})
}

 

 

posted @ 2019-11-14 17:18  可爱女孩lovely  阅读(...)  评论(... 编辑 收藏