Improved uploader

This commit is contained in:
Eduard Urbach 2018-04-17 13:16:53 +02:00
parent 0f28cdbc3d
commit 83d50548dc

View File

@ -1,23 +1,20 @@
export function fetchWithProgress(url, options: RequestInit, onProgress: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null): Promise<string> { export function fetchWithProgress(url, options: RequestInit, onProgress: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest() 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) xhr.open(options.method || "GET", url)
for(let k in options.headers || {}) { for(let k in options.headers || {}) {
xhr.setRequestHeader(k, options.headers[k]) 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) xhr.send(options.body)
}) })
} }