el-upload上传图片获取该图片的base64编码(转载)

开发中总会遇到一些无聊得需求(比如上传图片要base64)

于是找了大神的代码做个记录

原帖地址:https://blog.csdn.net/qq_35035975/article/details/108292088

 

HTML部分:

<el-upload action="#" :on-change="handleChangeUpload" list-type="picture-card" :auto-upload="false">
    <i class="el-icon-plus"></i>
</el-upload>

 

JS部分:

handleChangeUpload(file, fileList) {
                this.getBase64(file.raw).then(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 @ 2020-12-08 18:38  会飞的小白  阅读(1057)  评论(0编辑  收藏  举报