30 lines
815 B
TypeScript
30 lines
815 B
TypeScript
/** Trigger a client-side file download from a Blob. */
|
|
export function downloadBlob(filename: string, blob: Blob): void {
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = filename;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
// revoke on the next tick so the download has started
|
|
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
}
|
|
|
|
export function downloadText(
|
|
filename: string,
|
|
text: string,
|
|
mime = 'text/plain;charset=utf-8',
|
|
): void {
|
|
downloadBlob(filename, new Blob([text], { type: mime }));
|
|
}
|
|
|
|
export function downloadDataUrl(filename: string, dataUrl: string): void {
|
|
const a = document.createElement('a');
|
|
a.href = dataUrl;
|
|
a.download = filename;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
}
|