-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuploadFile.ts
38 lines (31 loc) · 1.01 KB
/
uploadFile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { showErrorAlert } from "./components/AlertManager";
export default async function uploadFile(
filter?: string
): Promise<{ name: string; result: string }> {
return new Promise<{ name: string; result: string }>((res, rej) => {
const input = document.createElement("input");
input.type = "file";
if (filter) input.accept = filter;
input.onchange = (_) => {
if (input.files?.length !== 1) {
return showErrorAlert("Expected only 1 file to be selected");
}
const file = input.files[0];
/*if (filter && !file.type.startsWith(filter)) {
return showErrorAlert(
"Invalid file type! Expected: " + filter + ", but got " + file.type
);
}*/
const reader = new FileReader();
reader.onload = (f) => {
if (!f.target || !f.target.DONE) return;
res({
name: file.name,
result: f.target.result?.toString() as string,
});
};
reader.readAsDataURL(file);
};
input.click();
});
}