- 分割base64字符串,获取base64的格式和ASCII字符串;
- 使用atob()方法将base64中的ASCII字符串解码成二进制数据"字符串";
- 将二进制数据按位放入8 位无符号整型数组中
- 适用new File()方法将ArrayBuffer转换成file对象
const base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFEAAABRCAYAAACqj0o2AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAC7SURBVHhe7dAxAQAwEAOh+jedivgbQQJvnEkMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSAxIDEgMSDzbPhRMI3enSuP1AAAAAElFTkSuQmCC"const base64ToFile = (base64: string) => {const baseArr = base64.split(',')const type = baseArr[0].match(/:(.*?);/)?.[1]const baseBuffer = atob(baseArr[1])let n = baseBuffer.lengthconst arrayBuffer = new Uint8Array(n)while(n--) {arrayBuffer[n] = baseBuffer.charCodeAt(n)}return new File([arrayBuffer], 'avatar.png', { type })}