diff --git a/scripts/Actions/Upload.ts b/scripts/Actions/Upload.ts index dee082a1..fdbbb1e5 100644 --- a/scripts/Actions/Upload.ts +++ b/scripts/Actions/Upload.ts @@ -1,6 +1,6 @@ import AnimeNotifier from "../AnimeNotifier" import StatusMessage from "../StatusMessage" -import { bytesHumanReadable, fetchWithProgress } from "../Utils" +import { bytesHumanReadable, uploadWithProgress } from "../Utils" // Select file export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) { @@ -65,7 +65,7 @@ function uploadFile(file: File, fileType: string, endpoint: string, arn: AnimeNo arn.statusMessage.showInfo(`Preparing to upload ${fileType} (${bytesHumanReadable(fileSize)})`, -1) try { - let responseText = await fetchWithProgress(endpoint, { + let responseText = await uploadWithProgress(endpoint, { method: "POST", credentials: "include", headers: { diff --git a/scripts/Utils/fetchWithProgress.ts b/scripts/Utils/fetchWithProgress.ts index 25438b66..017d7e61 100644 --- a/scripts/Utils/fetchWithProgress.ts +++ b/scripts/Utils/fetchWithProgress.ts @@ -1,12 +1,15 @@ -export function fetchWithProgress(url, options: RequestInit, onProgress: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null): Promise { +export function uploadWithProgress(url, options: RequestInit, onProgress: ((ev: ProgressEvent) => any) | null): Promise { return new Promise((resolve, reject) => { let xhr = new XMLHttpRequest() - xhr.addEventListener("load", e => resolve(xhr.responseText)) - xhr.addEventListener("error", reject) + xhr.onload = e => resolve(xhr.responseText) + xhr.onerror = reject if(onProgress && xhr.upload) { - xhr.upload.addEventListener("progress", onProgress) + xhr.upload.onprogress = onProgress + xhr.upload.onerror = reject + } else { + console.error("Could not attach progress event listener") } xhr.open(options.method || "GET", url, true)