From 627757ff6d30316a293df6293822a5c3b83291c1 Mon Sep 17 00:00:00 2001 From: ztimson Date: Fri, 19 Jul 2024 08:59:15 -0400 Subject: [PATCH] Added generics to upload function --- package.json | 2 +- src/files.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index c88976d..6c15334 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.14.0", + "version": "0.14.1", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/files.ts b/src/files.ts index 6179a5c..08b8e84 100644 --- a/src/files.ts +++ b/src/files.ts @@ -31,21 +31,21 @@ export function fileBrowser(options: {accept?: string, multiple?: boolean} = {}) }); } -export function uploadWithProgress(options: { +export function uploadWithProgress(options: { url: string; - file: File; + files: File[]; headers?: {[key: string]: string}; withCredentials?: boolean; -}) { - return new PromiseProgress((res, rej, prog) => { +}): PromiseProgress { + return new PromiseProgress((res, rej, prog) => { const xhr = new XMLHttpRequest(); const formData = new FormData(); - formData.append('file', options.file); + options.files.forEach(f => formData.append('file', f)); xhr.withCredentials = !!options.withCredentials Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value)); xhr.upload.addEventListener('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null); - xhr.upload.addEventListener('load', (resp) => res(resp)); + xhr.upload.addEventListener('load', (resp) => res(resp)); xhr.upload.addEventListener('error', (err) => rej(err)); xhr.open('POST', options.url);