zipfile_creation.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const startButton = document.getElementById('startButton');
  2. const statusDiv = document.getElementById('zipstatus');
  3. let pollingInterval = null;
  4. startButton.addEventListener('click', startZipCreation);
  5. function startZipCreation() {
  6. console.log(startButton.getAttribute('data-dir2zip'));
  7. const sourcePath = startButton.getAttribute('data-dir2zip');
  8. if (!sourcePath) {
  9. alert('Please enter the source path');
  10. return;
  11. }
  12. fetch('/start_zip', {
  13. method: 'POST',
  14. headers: { 'Content-Type': 'application/json' },
  15. body: JSON.stringify({ source_path: sourcePath })
  16. })
  17. .then(response => response.json())
  18. .then(data => {
  19. console.log(data);
  20. startPolling();
  21. })
  22. .catch(error => console.error('Error starting zip creation:', error));
  23. }
  24. function startPolling() {
  25. statusDiv.innerText = 'Zip creation in progress...';
  26. pollingInterval = setInterval(pollStatus, 1000);
  27. }
  28. function pollStatus() {
  29. fetch('/status')
  30. .then(response => response.json())
  31. .then(data => {
  32. if (data.status === 'completed') {
  33. clearInterval(pollingInterval);
  34. statusDiv.innerText = 'Zip creation completed. Downloading...';
  35. window.location.href = '/download';
  36. } else if (data.status === 'failed') {
  37. clearInterval(pollingInterval);
  38. statusDiv.innerText = `Error: ${data.error}`;
  39. } else {
  40. statusDiv.innerText = 'Zip creation in progress...';
  41. }
  42. })
  43. .catch(error => console.error('Error polling status:', error));
  44. }