From 8346eb9e052d10abf4fb65b66838aaab47152e2a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 30 Aug 2024 18:52:00 -0700 Subject: typescript --- src/lib/upload.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/lib/upload.ts (limited to 'src/lib/upload.ts') diff --git a/src/lib/upload.ts b/src/lib/upload.ts new file mode 100644 index 00000000..31c235c6 --- /dev/null +++ b/src/lib/upload.ts @@ -0,0 +1,18 @@ +export const upload = async (file, type, progress) => { + let url = `/api/upload/${type}`; + let formData = new FormData(); + formData.append("file", file); + + return new Promise((resolve, reject) => { + let ajax = new XMLHttpRequest(); + ajax.addEventListener("load", () => { + if (ajax.readyState === ajax.DONE) { + if (ajax.status === 200) resolve(ajax.response); + else reject(new Error(`Upload failed: ${ajax.response}`)); + } + }); + ajax.upload.addEventListener("progress", progress, false); + ajax.open("POST", url); + ajax.send(formData); + }); +}; -- cgit v1.2.3