import JSZip from 'jszip'; export interface DownloadUrlItem { name: string; url: string; } export function downloadFile(fileUrl: string, fileName: string): void { // if (fileUrl.includes('/media') && !fileUrl.startsWith('blob:')) { // fileUrl += fileUrl.includes('?') ? '&download=true' : '?download=true'; // } const a = document.createElement('a'); a.style.display = 'none'; a.href = fileUrl; a.download = fileName; document.body.appendChild(a); a.click(); document.body.removeChild(a); } export async function fetchBlob(fetchUrl: string): Promise { const response = await window.fetch(fetchUrl, { method: 'GET', headers: new Headers({ 'Access-Control-Allow-Origin': '*' }), }); const blob = await response.blob(); return blob; } export function downloadFiles(downloadUrlItems: DownloadUrlItem[], fileName?: string): void { if (!fileName) { fileName = new Date().getTime().toString() + ".zip"; } const zip = new JSZip(); for (let item of downloadUrlItems) { zip.file(item.name, fetchBlob(item.url)); } zip.generateAsync({ type: "blob" }).then(blob => { const url = window.URL.createObjectURL(blob); downloadFile(url, fileName); }); }