You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.2 KiB
44 lines
1.2 KiB
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<Blob> {
|
|
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);
|
|
});
|
|
}
|
|
|