Added generics to upload function
This commit is contained in:
parent
0a775f919b
commit
627757ff6d
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.14.0",
|
"version": "0.14.1",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
12
src/files.ts
12
src/files.ts
@ -31,21 +31,21 @@ export function fileBrowser(options: {accept?: string, multiple?: boolean} = {})
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function uploadWithProgress(options: {
|
export function uploadWithProgress<T>(options: {
|
||||||
url: string;
|
url: string;
|
||||||
file: File;
|
files: File[];
|
||||||
headers?: {[key: string]: string};
|
headers?: {[key: string]: string};
|
||||||
withCredentials?: boolean;
|
withCredentials?: boolean;
|
||||||
}) {
|
}): PromiseProgress<T> {
|
||||||
return new PromiseProgress((res, rej, prog) => {
|
return new PromiseProgress<T>((res, rej, prog) => {
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('file', options.file);
|
options.files.forEach(f => formData.append('file', f));
|
||||||
|
|
||||||
xhr.withCredentials = !!options.withCredentials
|
xhr.withCredentials = !!options.withCredentials
|
||||||
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
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('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
|
||||||
xhr.upload.addEventListener('load', (resp) => res(resp));
|
xhr.upload.addEventListener('load', (resp) => res(<any>resp));
|
||||||
xhr.upload.addEventListener('error', (err) => rej(err));
|
xhr.upload.addEventListener('error', (err) => rej(err));
|
||||||
|
|
||||||
xhr.open('POST', options.url);
|
xhr.open('POST', options.url);
|
||||||
|
Loading…
Reference in New Issue
Block a user