Fixed #74 (visual feedback of downloads)
This commit is contained in:
parent
b100435d9c
commit
a342def43f
1 changed files with 84 additions and 88 deletions
|
@ -208,7 +208,7 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||||
<th:block th:fragment="fileSelector(name, multiple)" th:with="accept=${accept} ?: '*/*', inputText=${inputText} ?: #{pdfPrompt}">
|
<th:block th:fragment="fileSelector(name, multiple)" th:with="accept=${accept} ?: '*/*', inputText=${inputText} ?: #{pdfPrompt}">
|
||||||
<script>
|
<script>
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
$('form').submit(function(event) {
|
$('form').submit(async function(event) {
|
||||||
const boredWaiting = localStorage.getItem('boredWaiting');
|
const boredWaiting = localStorage.getItem('boredWaiting');
|
||||||
if (boredWaiting === 'enabled') {
|
if (boredWaiting === 'enabled') {
|
||||||
$('#show-game-btn').show();
|
$('#show-game-btn').show();
|
||||||
|
@ -222,33 +222,35 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||||
var url = this.action;
|
var url = this.action;
|
||||||
console.log(url)
|
console.log(url)
|
||||||
event.preventDefault(); // Prevent the default form handling behavior
|
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);
|
|
||||||
} else {
|
|
||||||
console.log("start single download")
|
|
||||||
|
|
||||||
// Get the selected download option from localStorage
|
/* Check if ${multiple} is false */
|
||||||
const downloadOption = localStorage.getItem('downloadOption');
|
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")
|
||||||
|
await submitMultiPdfForm(event,url);
|
||||||
|
} else {
|
||||||
|
console.log("start single download")
|
||||||
|
|
||||||
var formData = new FormData($('form')[0]);
|
// Get the selected download option from localStorage
|
||||||
|
const downloadOption = localStorage.getItem('downloadOption');
|
||||||
// Send the request to the server using the fetch() API
|
|
||||||
fetch(url, {
|
var formData = new FormData($('form')[0]);
|
||||||
method: 'POST',
|
|
||||||
body: formData
|
// Send the request to the server using the fetch() API
|
||||||
}).then(response => {
|
const response = await fetch(url, {
|
||||||
if (!response) {
|
method: 'POST',
|
||||||
throw new Error('Received null response for file ' + i);
|
body: formData
|
||||||
}
|
});
|
||||||
console.log("load single download")
|
try {
|
||||||
|
if (!response) {
|
||||||
|
throw new Error('Received null response for file ' + i);
|
||||||
|
}
|
||||||
|
console.log("load single download")
|
||||||
|
|
||||||
|
|
||||||
// Extract the filename from the Content-Disposition header, if present
|
// Extract the filename from the Content-Disposition header, if present
|
||||||
let filename = null;
|
let filename = null;
|
||||||
const contentDispositionHeader = response.headers.get('Content-Disposition');
|
const contentDispositionHeader = response.headers.get('Content-Disposition');
|
||||||
console.log(contentDispositionHeader)
|
console.log(contentDispositionHeader)
|
||||||
|
@ -263,74 +265,68 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||||
|
|
||||||
const contentType = response.headers.get('Content-Type');
|
const contentType = response.headers.get('Content-Type');
|
||||||
console.log("contentType=" + contentType)
|
console.log("contentType=" + contentType)
|
||||||
// Check if the response is a PDF or an image
|
// Check if the response is a PDF or an image
|
||||||
if (contentType.includes('pdf') || contentType.includes('image')) {
|
if (contentType.includes('pdf') || contentType.includes('image')) {
|
||||||
response.blob().then(blob => {
|
const blob = await response.blob();
|
||||||
console.log("pdf/image")
|
console.log("pdf/image")
|
||||||
|
|
||||||
// Perform the appropriate action based on the download option
|
// Perform the appropriate action based on the download option
|
||||||
if (downloadOption === 'sameWindow') {
|
if (downloadOption === 'sameWindow') {
|
||||||
console.log("same window")
|
console.log("same window")
|
||||||
|
|
||||||
// Open the file in the same window
|
// Open the file in the same window
|
||||||
window.location.href = URL.createObjectURL(blob);
|
window.location.href = URL.createObjectURL(blob);
|
||||||
} else if (downloadOption === 'newWindow') {
|
} else if (downloadOption === 'newWindow') {
|
||||||
console.log("new window")
|
console.log("new window")
|
||||||
|
|
||||||
// Open the file in a new window
|
// Open the file in a new window
|
||||||
window.open(URL.createObjectURL(blob), '_blank');
|
window.open(URL.createObjectURL(blob), '_blank');
|
||||||
} else {
|
|
||||||
console.log("else save")
|
|
||||||
|
|
||||||
// Download the file
|
|
||||||
const link = document.createElement('a');
|
|
||||||
link.href = URL.createObjectURL(blob);
|
|
||||||
link.download = filename;
|
|
||||||
link.click();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else if (contentType.includes('json')) {
|
|
||||||
// Handle the JSON response
|
|
||||||
response.json().then(data => {
|
|
||||||
// Format the error message
|
|
||||||
const errorMessage = JSON.stringify(data, null, 2);
|
|
||||||
|
|
||||||
// Display the error message in an alert
|
|
||||||
alert(`An error occurred: ${errorMessage}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
response.blob().then(blob => {
|
console.log("else save")
|
||||||
console.log("else save 2 zip")
|
|
||||||
|
// Download the file
|
||||||
// For ZIP files or other file types, just download the file
|
const link = document.createElement('a');
|
||||||
const link = document.createElement('a');
|
link.href = URL.createObjectURL(blob);
|
||||||
link.href = URL.createObjectURL(blob);
|
link.download = filename;
|
||||||
link.download = filename;
|
link.click();
|
||||||
link.click();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
})
|
} else if (contentType.includes('json')) {
|
||||||
.catch(error => {
|
// Handle the JSON response
|
||||||
console.log("error listener")
|
const json = await response.json();
|
||||||
|
// Format the error message
|
||||||
// Extract the error message and stack trace from the response
|
const errorMessage = JSON.stringify(json, null, 2);
|
||||||
const errorMessage = error.message;
|
// Display the error message in an alert
|
||||||
const stackTrace = error.stack;
|
alert(`An error occurred: ${errorMessage}`);
|
||||||
|
} else {
|
||||||
// Create an error message to display to the user
|
const blob = await response.blob()
|
||||||
const message = `${errorMessage}\n\n${stackTrace}`;
|
console.log("else save 2 zip")
|
||||||
|
|
||||||
$('#submitBtn').text(submitButtonText);
|
|
||||||
|
|
||||||
// Display the error message to the user
|
// For ZIP files or other file types, just download the file
|
||||||
alert(message);
|
const link = document.createElement('a');
|
||||||
|
link.href = URL.createObjectURL(blob);
|
||||||
});
|
link.download = filename;
|
||||||
|
link.click();
|
||||||
|
}
|
||||||
|
} catch(error) {
|
||||||
|
console.log("error listener")
|
||||||
|
|
||||||
|
// Extract the error message and stack trace from the response
|
||||||
|
const errorMessage = error.message;
|
||||||
|
const stackTrace = error.stack;
|
||||||
|
|
||||||
|
// Create an error message to display to the user
|
||||||
|
const message = `${errorMessage}\n\n${stackTrace}`;
|
||||||
|
|
||||||
|
$('#submitBtn').text(submitButtonText);
|
||||||
|
|
||||||
}
|
// Display the error message to the user
|
||||||
$('#submitBtn').text(submitButtonText);
|
alert(message);
|
||||||
});
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
$('#submitBtn').text(submitButtonText);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
async function submitMultiPdfForm(event, url) {
|
async function submitMultiPdfForm(event, url) {
|
||||||
// Get the selected PDF files
|
// Get the selected PDF files
|
||||||
|
|
Loading…
Reference in a new issue