Improved file upload

This commit is contained in:
2018-04-16 21:41:05 +02:00
parent 889a9700ad
commit bb15daae6b
2 changed files with 45 additions and 15 deletions

View File

@ -0,0 +1,19 @@
export function fetchWithProgress(url, opts: RequestInit, onProgress: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null): Promise<string> {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest()
xhr.open(opts.method || "GET", url)
for(let k in opts.headers || {}) {
xhr.setRequestHeader(k, opts.headers[k])
}
xhr.onload = e => resolve(xhr.responseText)
xhr.onerror = reject
if(xhr.upload && onProgress) {
xhr.upload.onprogress = onProgress
}
xhr.send(opts.body)
})
}