Reject upload when status code is faulty
This commit is contained in:
29
scripts/Utils/uploadWithProgress.ts
Normal file
29
scripts/Utils/uploadWithProgress.ts
Normal file
@ -0,0 +1,29 @@
|
||||
export function uploadWithProgress(url, options: RequestInit, onProgress: ((ev: ProgressEvent) => any) | null): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let xhr = new XMLHttpRequest()
|
||||
|
||||
xhr.onload = () => {
|
||||
if(xhr.status >= 400) {
|
||||
return reject(xhr.responseText)
|
||||
}
|
||||
|
||||
resolve(xhr.responseText)
|
||||
}
|
||||
xhr.onerror = reject
|
||||
|
||||
if(onProgress && xhr.upload) {
|
||||
xhr.upload.onprogress = onProgress
|
||||
xhr.upload.onerror = reject
|
||||
} else {
|
||||
console.error("Could not attach progress event listener")
|
||||
}
|
||||
|
||||
xhr.open(options.method || "GET", url, true)
|
||||
|
||||
for(let k in options.headers || {}) {
|
||||
xhr.setRequestHeader(k, options.headers[k])
|
||||
}
|
||||
|
||||
xhr.send(options.body)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user