feat(04-02): add downloadFile and downloadZip utility functions
- src/utils/downloadFile.ts: Blob + createObjectURL + anchor click download - src/utils/downloadZip.ts: JSZip archive builder with same Blob download pattern - npm install jszip ^3.10.1 (only new runtime dependency in phase 4)
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
// src/utils/downloadFile.ts
|
||||
// Triggers a browser file download from a string.
|
||||
// Pattern: MDN URL.createObjectURL — https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL_static
|
||||
export function downloadFile(content: string, filename: string): void {
|
||||
const blob = new Blob([content], { type: 'text/plain;charset=utf-8' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// src/utils/downloadZip.ts
|
||||
// Builds a ZIP archive from file entries and triggers download.
|
||||
// Pattern: JSZip official docs — https://stuk.github.io/jszip/documentation/examples/download-zip-file.html
|
||||
import JSZip from 'jszip';
|
||||
|
||||
export async function downloadZip(
|
||||
files: { name: string; content: string }[],
|
||||
zipName: string
|
||||
): Promise<void> {
|
||||
const zip = new JSZip();
|
||||
for (const { name, content } of files) {
|
||||
zip.file(name, content);
|
||||
}
|
||||
const blob = await zip.generateAsync({ type: 'blob' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = zipName;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
Reference in New Issue
Block a user