Fixed #74 (visual feedback of downloads)

This commit is contained in:
Saud Fatayerji 2023-05-04 20:48:57 +03:00
parent b100435d9c
commit a342def43f

View file

@ -208,7 +208,7 @@ document.addEventListener("DOMContentLoaded", function () {
<th:block th:fragment="fileSelector(name, multiple)" th:with="accept=${accept} ?: '*/*', inputText=${inputText} ?: #{pdfPrompt}">
<script>
$(document).ready(function() {
$('form').submit(function(event) {
$('form').submit(async function(event) {
const boredWaiting = localStorage.getItem('boredWaiting');
if (boredWaiting === 'enabled') {
$('#show-game-btn').show();
@ -222,13 +222,14 @@ document.addEventListener("DOMContentLoaded", function () {
var url = this.action;
console.log(url)
event.preventDefault(); // Prevent the default form handling behavior
/* Check if ${multiple} is false */
var multiple = [[${multiple}]] || false;
var override = $('#override').val() || '';
console.log("override=" + override)
if (override === 'multi' || (!multiple && files.length > 1) && override !== 'single' ) {
console.log("multi parallel download")
submitMultiPdfForm(event,url);
await submitMultiPdfForm(event,url);
} else {
console.log("start single download")
@ -238,10 +239,11 @@ document.addEventListener("DOMContentLoaded", function () {
var formData = new FormData($('form')[0]);
// Send the request to the server using the fetch() API
fetch(url, {
const response = await fetch(url, {
method: 'POST',
body: formData
}).then(response => {
});
try {
if (!response) {
throw new Error('Received null response for file ' + i);
}
@ -265,7 +267,7 @@ document.addEventListener("DOMContentLoaded", function () {
console.log("contentType=" + contentType)
// Check if the response is a PDF or an image
if (contentType.includes('pdf') || contentType.includes('image')) {
response.blob().then(blob => {
const blob = await response.blob();
console.log("pdf/image")
// Perform the appropriate action based on the download option
@ -288,19 +290,15 @@ document.addEventListener("DOMContentLoaded", function () {
link.download = filename;
link.click();
}
});
} else if (contentType.includes('json')) {
// Handle the JSON response
response.json().then(data => {
const json = await response.json();
// Format the error message
const errorMessage = JSON.stringify(data, null, 2);
const errorMessage = JSON.stringify(json, null, 2);
// Display the error message in an alert
alert(`An error occurred: ${errorMessage}`);
});
} else {
response.blob().then(blob => {
const blob = await response.blob()
console.log("else save 2 zip")
// For ZIP files or other file types, just download the file
@ -308,10 +306,8 @@ document.addEventListener("DOMContentLoaded", function () {
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
});
}
})
.catch(error => {
} catch(error) {
console.log("error listener")
// Extract the error message and stack trace from the response
@ -326,7 +322,7 @@ document.addEventListener("DOMContentLoaded", function () {
// Display the error message to the user
alert(message);
});
};
}
$('#submitBtn').text(submitButtonText);