downloadBlob
EditStarts a browser download for an in-memory Blob.
/** Starts a browser download for a Blob and releases its temporary object URL. */export const downloadBlob = (blob: Blob, filename: string): void => { if (filename.trim().length === 0) throw new TypeError("filename cannot be empty"); const url = URL.createObjectURL(blob); const anchor = document.createElement("a"); anchor.href = url; anchor.download = filename; anchor.style.display = "none"; document.body.append(anchor); try { anchor.click(); } finally { anchor.remove(); URL.revokeObjectURL(url); }};Download
Section titled “Download”wget -O src/lib/downloadBlob.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/downloadBlob.tstemp_dir="$(mktemp -d)" && bunx degit jrTilak/lazykit/registry/functions "$temp_dir" && mv "$temp_dir/downloadBlob.ts" src/lib/downloadBlob.ts && rm -r "$temp_dir"Examples
Section titled “Examples”import { downloadBlob } from "./downloadBlob";
const report = new Blob(["name,total\nAda,42"], { type: "text/csv" });downloadBlob(report, "report.csv");blob(Blob) — Browser data to download.filename(string) — Non-empty suggested filename.- The temporary anchor and object URL are always released.