From fa0c998d0fdc9755cf18c34c4e109f17cd1b61e9 Mon Sep 17 00:00:00 2001 From: Toni <81181280+Git-Peacock@users.noreply.github.com> Date: Mon, 9 Feb 2026 16:27:58 +0100 Subject: [PATCH] upload progress bar --- public/app.js | 79 ++++++++++++++++++++++++++++++++++++++++++----- public/index.html | 4 +++ public/style.css | 34 ++++++++++++++++++++ 3 files changed, 110 insertions(+), 7 deletions(-) diff --git a/public/app.js b/public/app.js index 96f1a8a..9ea479b 100644 --- a/public/app.js +++ b/public/app.js @@ -385,22 +385,87 @@ dropZone.addEventListener('drop', (e) => { } }); +// File Input Change +document.getElementById('file-input').addEventListener('change', (e) => { + if (e.target.files.length > 0) { + uploadFiles(e.target.files); + // Reset input so same file can be selected again if needed + e.target.value = ''; + } +}); + async function uploadFiles(files) { const statusDiv = document.getElementById('upload-status'); + const progressContainer = document.getElementById('progress-container'); + const progressBar = document.getElementById('progress-bar'); + const progressText = document.getElementById('progress-text'); + statusDiv.innerHTML = 'Uploading...'; + progressContainer.classList.remove('hidden'); + progressBar.style.width = '0%'; + progressText.textContent = '0%'; + + let totalSize = 0; + for (let file of files) totalSize += file.size; + + // Avoid division by zero + if (totalSize === 0) totalSize = 1; + + let previousFilesSize = 0; for (let file of files) { - const formData = new FormData(); - formData.append('file', file); - if (currentPath) { - await fetch(`/api/upload?path=${encodeURIComponent(currentPath)}`, { method: 'POST', body: formData }); - } else { - await fetch('/api/upload', { method: 'POST', body: formData }); + try { + await new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + const formData = new FormData(); + formData.append('file', file); + + const url = currentPath + ? `/api/upload?path=${encodeURIComponent(currentPath)}` + : '/api/upload'; + + xhr.open('POST', url); + + xhr.upload.onprogress = (e) => { + if (e.lengthComputable) { + const currentLoaded = e.loaded; + const totalLoaded = previousFilesSize + currentLoaded; + const percent = Math.min(100, Math.round((totalLoaded / totalSize) * 100)); + progressBar.style.width = percent + '%'; + progressText.textContent = percent + '%'; + } + }; + + xhr.onload = () => { + if (xhr.status >= 200 && xhr.status < 300) { + resolve(); + } else { + reject(new Error(`Upload failed: ${xhr.statusText}`)); + } + }; + + xhr.onerror = () => reject(new Error('Network Error')); + + xhr.send(formData); + }); + previousFilesSize += file.size; + } catch (e) { + console.error(e); + statusDiv.innerHTML = `Error uploading ${file.name}`; } } + progressBar.style.width = '100%'; + progressText.textContent = '100%'; statusDiv.innerHTML = 'Done!'; - setTimeout(() => statusDiv.innerHTML = '', 2000); + + setTimeout(() => { + statusDiv.innerHTML = ''; + progressContainer.classList.add('hidden'); + progressBar.style.width = '0%'; + progressText.textContent = '0%'; + }, 2000); + loadFiles(); } diff --git a/public/index.html b/public/index.html index 08490f5..97363d3 100644 --- a/public/index.html +++ b/public/index.html @@ -67,6 +67,10 @@ Files
+