export const getBase64 = () => {
return new Promise((resolve, reject) => {
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
input.onchange = (event) => {
const file = event.target.files[0];
const isJPG = file.type === 'image/jpeg';
const isPNG = file.type === 'image/png';
if (!isJPG && !isPNG) {
showErrorAlert('只支持上传 JPG/PNG 格式的图片');
reject();
} else {
const reader = new FileReader();
reader.onload = (event) => {
const fileData = event.target.result; // 获取图片数据
resolve(fileData)
};
reader.readAsDataURL(file);
}
};
input.click();
});
}
执行方法 将图片转base64
最新推荐文章于 2024-11-01 13:37:40 发布