Updated download functions & added CSV serializer
All checks were successful
Build / Build NPM Project (push) Successful in 25s
Build / Tag Version (push) Successful in 6s

This commit is contained in:
2024-09-22 03:35:12 -04:00
parent 3bf8c7bd09
commit 51549db3d9
5 changed files with 59 additions and 28 deletions

View File

@ -1,33 +1,35 @@
import {makeArray} from './array.ts';
import {JSONAttemptParse} from './objects.ts';
import {PromiseProgress} from './promise-progress';
/**
* Download a file from a URL
*
* @param href URL that will be downloaded
* @param {string} name Override download name
*/
export function download(href: any, name?: string) {
const a = document.createElement('a');
a.href = href;
a.download = name || href.split('/').pop();
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
/**
* Download blob as a file
*
* @param {Blob} blob File as a blob
* @param {string} name Name blob will be downloaded as
*/
export function downloadBlob(blob: Blob, name: string) {
export function downloadFile(blob: Blob | string | string[], name: string) {
if(!(blob instanceof Blob)) blob = new Blob(makeArray(blob));
const url = URL.createObjectURL(blob);
download(url, name);
downloadUrl(url, name);
URL.revokeObjectURL(url);
}
/**
* Download a file from a URL
*
* @param href URL that will be downloaded
* @param {string} name Override download name
*/
export function downloadUrl(href: any, name?: string) {
const a = document.createElement('a');
a.href = href;
a.download = name || href.split('/').pop();
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
/**
* Open filebrowser & return selected file
*