e4a76e96af
* CSS corrections * HTML corrections * JS corrections * JAVA corrections * remove tab * CSS corrections 2 * JS corrections 2 * back to the roots * max-linie 127 * add slash hr|br * return bootstrap-icons.css * return bootstrap-icons.min.css * return bootstrap.min.css * Update bootstrap-icons.css * Update bootstrap-icons.min.css * Update bootstrap-icons.min.css * Update bootstrap.min.css * CSS corrections * HTML corrections * JS corrections * JAVA corrections * remove tab * CSS corrections 2 * JS corrections 2 * back to the roots * max-linie 127 * add slash hr|br * return bootstrap-icons.css * Update bootstrap-icons.css * Bootstrap CSS * Update prism.css
47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
async function downloadFilesWithCallback(processFileCallback) {
|
|
const fileInput = document.querySelector('input[type="file"]');
|
|
const files = fileInput.files;
|
|
|
|
const zipThreshold = 4;
|
|
const zipFiles = files.length > zipThreshold;
|
|
|
|
let jszip = null;
|
|
if (zipFiles) {
|
|
jszip = new JSZip();
|
|
}
|
|
|
|
const promises = Array.from(files).map(async (file) => {
|
|
const { processedData, fileName } = await processFileCallback(file);
|
|
|
|
if (zipFiles) {
|
|
jszip.file(fileName, processedData);
|
|
} else {
|
|
const url = URL.createObjectURL(processedData);
|
|
const downloadOption = localStorage.getItem("downloadOption");
|
|
|
|
if (downloadOption === "sameWindow") {
|
|
window.location.href = url;
|
|
} else if (downloadOption === "newWindow") {
|
|
window.open(url, "_blank");
|
|
} else {
|
|
const downloadLink = document.createElement("a");
|
|
downloadLink.href = url;
|
|
downloadLink.download = fileName;
|
|
downloadLink.click();
|
|
}
|
|
}
|
|
});
|
|
|
|
await Promise.all(promises);
|
|
|
|
if (zipFiles) {
|
|
const content = await jszip.generateAsync({ type: "blob" });
|
|
const url = URL.createObjectURL(content);
|
|
const a = document.createElement("a");
|
|
a.href = url;
|
|
a.download = "files.zip";
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
}
|
|
}
|