场景:
el-upload 上传图片,读取文件成base64的时候,console报错:
Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'
报错代码:
javascript">
/**
* File转base64
* @param file
* @return base64
* */
export function getBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
let fileResult = "";
reader.readAsDataURL(file);
//开始转
reader.onload = () => {
fileResult = reader.result;
};
//转失败
reader.onerror = (error) => {
reject(error);
};
//结束 resolve
reader.onloadend = () => {
resolve(fileResult);
};
});
}
解决:
这里不能直接用 reader.readAsDataURL(file);