This commit is contained in:
Anthony Stirling 2023-03-03 00:14:55 +00:00
parent 40e4fbabe6
commit 721645273c

View File

@ -34,7 +34,6 @@
</div> </div>
<script> <script>
$('#multiPdfForm').submit(function(event) { $('#multiPdfForm').submit(function(event) {
event.preventDefault(); // Prevent the default form handling behavior event.preventDefault(); // Prevent the default form handling behavior
@ -42,61 +41,80 @@
submitMultiPdfForm(event); submitMultiPdfForm(event);
}); });
async function submitMultiPdfForm(event) { async function submitMultiPdfForm(event) {
// Get the selected PDF files // Get the selected PDF files
var files = $('#fileInput-input')[0].files; var files = $('#fileInput-input')[0].files;
// Get the existing form data // Get the existing form data
var formData = new FormData($('form')[0]); var formData = new FormData($('form')[0]);
formData.delete('fileInput'); formData.delete('fileInput');
// Show the progress bar // Show the progress bar
$('#progressBarContainer').show(); $('#progressBarContainer').show();
// Submit each PDF file sequentially // Initialize the progress bar
var progressBar = $('#progressBar');
progressBar.css('width', '0%');
progressBar.attr('aria-valuenow', 0);
progressBar.attr('aria-valuemax', files.length);
// Submit each PDF file in parallel
var promises = [];
for (var i = 0; i < files.length; i++) { for (var i = 0; i < files.length; i++) {
formData.append('fileInput', files[i]); var promise = new Promise(function(resolve, reject) {
var fileFormData = new FormData();
fileFormData.append('fileInput', files[i]);
fileFormData.append('format', $('select[name="format"]').val());
console.log('Submitting request for file ' + i); fetch('extract-images', {
method: 'POST',
body: fileFormData
}).then(function(response) {
console.log('Received response for file ' + i + ': ' + response);
// Initialize the progress bar var contentDisposition = response.headers.get('content-disposition');
var progressBar = $('#progressBar'); var fileName = contentDisposition.split('filename=')[1].replace(/"/g, '');
progressBar.css('width', '0%');
progressBar.attr('aria-valuenow', 0);
progressBar.attr('aria-valuemax', files.length);
await fetch('extract-images', {
method: 'POST',
body: formData
}).then(function(response) {
console.log('Received response for file ' + i + ': ' + response);
var contentDisposition = response.headers.get('content-disposition'); response.blob().then(function(blob) {
var fileName = contentDisposition.split('filename=')[1].replace(/"/g, ''); var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
response.blob().then(function(blob) { a.href = url;
var url = window.URL.createObjectURL(blob); a.download = fileName;
var a = document.createElement('a'); document.body.appendChild(a);
a.href = url; a.click();
a.download = fileName; a.remove();
document.body.appendChild(a); resolve();
a.click(); });
a.remove(); }).catch(function(error) {
console.error('Error submitting request for file ' + i + ': ' + error);
reject();
}); });
}).catch(function(error) {
console.error('Error submitting request for file ' + i + ': ' + error);
}).finally(function() {
// Update the progress bar
var progress = Math.round(((i + 1) / files.length) * 100);
console.log('progress ' + progress);
progressBar.css('width', progress + '%');
progressBar.attr('aria-valuenow', i + 1);
}); });
// Update the progress bar as each request finishes
promise.then(function() {
var progress = ((progressBar.attr('aria-valuenow') / files.length) * 100) + (100 / files.length);
progressBar.css('width', progress + '%');
progressBar.attr('aria-valuenow', parseInt(progressBar.attr('aria-valuenow')) + 1);
});
promises.push(promise);
} }
// Wait for all requests to finish
try {
await Promise.all(promises);
} catch (error) {
console.error('Error while uploading files: ' + error);
}
// Update the progress bar
progressBar.css('width', '100%');
progressBar.attr('aria-valuenow', files.length);
} }