Die DownloadFiles-Funktion:
Code: Select all
const downloadFiles = async (items: typeof miscellaneousData) => {
if (items.length === 1) {
const link = document.createElement('a');
link.href = items[0].downloadLink;
link.setAttribute('download', '');
link.style.display = 'none';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
return;
}
const zip = new JSZip();
try {
items.forEach((item) => {
const fileName = item.downloadLink.split('/').pop() || 'document.docx';
zip.file(fileName, item.downloadLink);
});
const content = await zip.generateAsync({ type: 'blob' });
const zipUrl = URL.createObjectURL(content);
const link = document.createElement('a');
link.href = zipUrl;
link.download = 'templates.zip';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(zipUrl);
} catch (error) {
console.error('Error creating zip:', error);
items.forEach((item, index) => {
setTimeout(() => {
const link = document.createElement('a');
link.href = item.downloadLink;
link.setAttribute('download', '');
link.style.display = 'none';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}, index * 1000);
});
}
};
Ich möchte das natürlich, damit es in der Produktion Dateien sicher und unbeschädigt herunterlädt.
Die Dateien werden als URL in der Codebasis gespeichert/eingegeben, und ich glaube, meine Funktion extrahiert die URL statt den Dateiinhalt. Ich habe jedoch versucht, stattdessen den Dateiinhalt zu extrahieren, aber es werden dieselben Probleme oder fehlerhafte Funktionen angezeigt, z. B. dass kein Massen-Download möglich ist usw.