2024-08-17 14:21:05 -04:00
|
|
|
import {deepCopy, JSONAttemptParse} from './objects.ts';
|
2024-07-19 09:02:42 -04:00
|
|
|
import {PromiseProgress} from './promise-progress';
|
2024-07-19 08:45:33 -04:00
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-19 08:59:15 -04:00
|
|
|
export function uploadWithProgress<T>(options: {
|
2024-07-19 08:45:33 -04:00
|
|
|
url: string;
|
2024-07-19 08:59:15 -04:00
|
|
|
files: File[];
|
2024-07-19 08:45:33 -04:00
|
|
|
headers?: {[key: string]: string};
|
|
|
|
withCredentials?: boolean;
|
2024-07-19 08:59:15 -04:00
|
|
|
}): PromiseProgress<T> {
|
|
|
|
return new PromiseProgress<T>((res, rej, prog) => {
|
2024-07-19 08:45:33 -04:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
const formData = new FormData();
|
2024-07-19 08:59:15 -04:00
|
|
|
options.files.forEach(f => formData.append('file', f));
|
2024-07-19 08:45:33 -04:00
|
|
|
|
2024-08-17 14:21:05 -04:00
|
|
|
xhr.withCredentials = !!options.withCredentials;
|
2024-07-19 08:45:33 -04:00
|
|
|
xhr.upload.addEventListener('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
|
2024-08-17 14:21:05 -04:00
|
|
|
xhr.addEventListener('loadend', () => res(<T>JSONAttemptParse(xhr.responseText)));
|
|
|
|
xhr.addEventListener('error', () => rej(JSONAttemptParse(xhr.responseText)));
|
|
|
|
xhr.addEventListener('timeout', () => rej({error: 'Request timed out'}));
|
2024-07-19 08:45:33 -04:00
|
|
|
|
|
|
|
xhr.open('POST', options.url);
|
2024-07-19 10:16:31 -04:00
|
|
|
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
2024-07-19 08:45:33 -04:00
|
|
|
xhr.send(formData);
|
|
|
|
});
|
|
|
|
}
|