mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-09-03 17:52:30 +02:00
async
This commit is contained in:
parent
40e4fbabe6
commit
721645273c
@ -34,7 +34,6 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
|
||||
$('#multiPdfForm').submit(function(event) {
|
||||
event.preventDefault(); // Prevent the default form handling behavior
|
||||
|
||||
@ -42,61 +41,80 @@
|
||||
submitMultiPdfForm(event);
|
||||
});
|
||||
|
||||
|
||||
|
||||
async function submitMultiPdfForm(event) {
|
||||
|
||||
|
||||
// Get the selected PDF files
|
||||
var files = $('#fileInput-input')[0].files;
|
||||
|
||||
// Get the existing form data
|
||||
var formData = new FormData($('form')[0]);
|
||||
formData.delete('fileInput');
|
||||
|
||||
|
||||
// Show the progress bar
|
||||
$('#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++) {
|
||||
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 progressBar = $('#progressBar');
|
||||
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');
|
||||
var fileName = contentDisposition.split('filename=')[1].replace(/"/g, '');
|
||||
|
||||
var contentDisposition = response.headers.get('content-disposition');
|
||||
var fileName = contentDisposition.split('filename=')[1].replace(/"/g, '');
|
||||
|
||||
response.blob().then(function(blob) {
|
||||
var url = window.URL.createObjectURL(blob);
|
||||
var a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
response.blob().then(function(blob) {
|
||||
var url = window.URL.createObjectURL(blob);
|
||||
var a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
resolve();
|
||||
});
|
||||
}).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);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user