From 83d50548dc28f0b403f1849b4b56f14c5cc392f2 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 17 Apr 2018 13:16:53 +0200 Subject: [PATCH] Improved uploader --- scripts/Utils/fetchWithProgress.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/scripts/Utils/fetchWithProgress.ts b/scripts/Utils/fetchWithProgress.ts index 224a61b3..ed31094b 100644 --- a/scripts/Utils/fetchWithProgress.ts +++ b/scripts/Utils/fetchWithProgress.ts @@ -1,23 +1,20 @@ export function fetchWithProgress(url, options: RequestInit, onProgress: ((this: XMLHttpRequest, 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) + + if(onProgress && xhr.upload) { + xhr.upload.addEventListener("progress", onProgress) + } + xhr.open(options.method || "GET", url) for(let k in options.headers || {}) { xhr.setRequestHeader(k, options.headers[k]) } - xhr.onload = e => resolve(xhr.responseText) - xhr.onerror = reject - - if(onProgress) { - xhr.addEventListener("progress", onProgress) - - if(xhr.upload) { - xhr.upload.onprogress = onProgress - } - } - xhr.send(options.body) }) }