Minor changes

This commit is contained in:
Eduard Urbach 2018-04-17 14:41:31 +02:00
parent 68ac241106
commit 0856fc209a
2 changed files with 9 additions and 6 deletions

View File

@ -1,6 +1,6 @@
import AnimeNotifier from "../AnimeNotifier" import AnimeNotifier from "../AnimeNotifier"
import StatusMessage from "../StatusMessage" import StatusMessage from "../StatusMessage"
import { bytesHumanReadable, fetchWithProgress } from "../Utils" import { bytesHumanReadable, uploadWithProgress } from "../Utils"
// Select file // Select file
export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) { 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) arn.statusMessage.showInfo(`Preparing to upload ${fileType} (${bytesHumanReadable(fileSize)})`, -1)
try { try {
let responseText = await fetchWithProgress(endpoint, { let responseText = await uploadWithProgress(endpoint, {
method: "POST", method: "POST",
credentials: "include", credentials: "include",
headers: { headers: {

View File

@ -1,12 +1,15 @@
export function fetchWithProgress(url, options: RequestInit, onProgress: ((this: XMLHttpRequest, ev: ProgressEvent) => any) | null): Promise<string> { export function uploadWithProgress(url, options: RequestInit, onProgress: ((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.onload = e => resolve(xhr.responseText)
xhr.addEventListener("error", reject) xhr.onerror = reject
if(onProgress && xhr.upload) { 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) xhr.open(options.method || "GET", url, true)