fixes file handling.

This commit is contained in:
Michael Beck
2025-02-06 00:49:41 +01:00
parent 587bed201e
commit dfe305272c
4 changed files with 91 additions and 13 deletions

View File

@@ -2,9 +2,9 @@ function deleteFiles(filePaths) {
fetch('/delete_files', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Type': 'application/json',
},
body: new URLSearchParams({
body: JSON.stringify({
'file_paths': filePaths
})
})
@@ -29,18 +29,37 @@ function deleteSelectedFiles() {
}
}
function downloadSelectedFiles() {
const selectedFiles = Array.from(document.querySelectorAll('input[name="fileCheckbox"]:checked'))
.map(checkbox => checkbox.value);
if (selectedFiles.length > 0) {
selectedFiles.forEach(file => {
const link = document.createElement('a');
link.href = file;
link.download = file.split('/').pop();
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
fetch('/download_files', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ 'file_paths': selectedFiles })
})
.then(response => {
if (!response.ok) {
return response.json().then(err => { throw new Error(err.error); });
}
return response.blob();
})
.then(blob => {
if (blob.type !== 'application/zip') {
throw new Error("Received invalid ZIP file.");
}
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'files.zip';
document.body.appendChild(a);
a.click();
a.remove();
})
.catch(error => alert('Error: ' + error.message));
} else {
alert('No files selected');
}