vue项目开发时需要将选择的文件转为base64编码上传,所以需要在el-upload选择文件后,将其转为base64编码
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);
};
});
},