Added filebrowser & Merged a bunch of functions into file utilities
All checks were successful
Build / Build NPM Project (push) Successful in 23s
Build / Tag Version (push) Successful in 4s

This commit is contained in:
Zakary Timson 2024-07-19 08:45:33 -04:00
parent 7063b80bdd
commit 0a775f919b
5 changed files with 56 additions and 42 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.13.3",
"version": "0.14.0",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -1,14 +0,0 @@
export function download(href: any, name: string) {
const a = document.createElement('a');
a.href = href;
a.download = name;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
export function downloadBlob(blob: Blob, name: string) {
const url = URL.createObjectURL(blob);
download(url, name);
URL.revokeObjectURL(url);
}

54
src/files.ts Normal file
View File

@ -0,0 +1,54 @@
import {PromiseProgress} from './promise-progress.ts';
export function download(href: any, name: string) {
const a = document.createElement('a');
a.href = href;
a.download = name;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
export function downloadBlob(blob: Blob, name: string) {
const url = URL.createObjectURL(blob);
download(url, name);
URL.revokeObjectURL(url);
}
export function fileBrowser(options: {accept?: string, multiple?: boolean} = {}): Promise<File[]> {
return new Promise(res => {
const input = document.createElement('input');
input.type = 'file';
input.accept = options.accept || '*';
input.style.display='none';
input.multiple = !!options.multiple;
input.onblur = input.onchange = async () => {
res(Array.from(<any>input.files));
input.remove();
}
document.body.appendChild(input);
input.click();
});
}
export function uploadWithProgress(options: {
url: string;
file: File;
headers?: {[key: string]: string};
withCredentials?: boolean;
}) {
return new PromiseProgress((res, rej, prog) => {
const xhr = new XMLHttpRequest();
const formData = new FormData();
formData.append('file', options.file);
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('error', (err) => rej(err));
xhr.open('POST', options.url);
xhr.send(formData);
});
}

View File

@ -1,6 +1,6 @@
export * from './array';
export * from './aset';
export * from './download';
export * from './files';
export * from './emitter';
export * from './errors';
export * from './http';
@ -11,4 +11,3 @@ export * from './objects';
export * from './promise-progress';
export * from './string';
export * from './time';
export * from './upload';

View File

@ -1,25 +0,0 @@
import {PromiseProgress} from './promise-progress';
export type UploadOptions = {
url: string;
file: File;
headers?: {[key: string]: string};
withCredentials?: boolean;
}
export function uploadWithProgress(options: UploadOptions) {
return new PromiseProgress((res, rej, prog) => {
const xhr = new XMLHttpRequest();
const formData = new FormData();
formData.append('file', options.file);
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('error', (err) => rej(err));
xhr.open('POST', options.url);
xhr.send(formData);
});
}