开发中总会遇到一些无聊得需求(比如上传图片要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); }; }); },