Skip to content

Commit 394890c

Browse files
author
Simon he
committed
chore: add base64ToFile
1 parent 316bbef commit 394890c

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/base64ToFile.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function base64ToFile(s: string, filename: string): File {
2+
const arr = s.split(','),
3+
mime = arr[0]?.match(/:(.*?);/)?.[1],
4+
bstr = atob(arr[1])
5+
let n = bstr.length
6+
const u8arr = new Uint8Array(n);
7+
while (n--) {
8+
u8arr[n] = bstr.charCodeAt(n);
9+
}
10+
return new File([u8arr], filename, { type: mime });
11+
}

0 commit comments

Comments
 (0)