fix: show upload phase immediately + fallback for non-computable progress

Added loadstart handler to show 'Uploading...' spinner as soon as
upload begins, plus fallback label when lengthComputable is false
(shows bytes uploaded even without percentage).
This commit is contained in:
Claus Lohmar 2026-07-21 19:43:04 +00:00
parent 5174939939
commit 5ff0635aa5

View file

@ -128,17 +128,31 @@ async function startSession(e) {
} }
btn.disabled = false; btn.disabled = false;
} }
async function handleUpload(formData) { async function handleUpload(formData) {
return new Promise((resolve) => { return new Promise((resolve) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open('POST', '/session/upload'); xhr.open('POST', '/session/upload');
xhr.timeout = 7200000; // 2 hour timeout for very large uploads
xhr.upload.addEventListener('progress', (e) => { xhr.upload.addEventListener('progress', (e) => {
if (e.lengthComputable) { if (e.lengthComputable) {
const pct = Math.round((e.loaded / e.total) * 100); const pct = Math.round((e.loaded / e.total) * 100);
setPhase('Uploading source file... ' + (e.loaded / (1024**3)).toFixed(1) + ' GiB', pct, false); setPhase('Uploading source file... ' + (e.loaded / (1024**3)).toFixed(1) + ' GiB', pct, false);
} else {
// Fallback: show bytes uploaded even without total
setPhase('Uploading source file... ' + (e.loaded / (1024**3)).toFixed(1) + ' GiB', 0, true);
} }
}); });
xhr.addEventListener('loadstart', () => {
setPhase('Uploading source file...', 0, true);
});
xhr.addEventListener('timeout', () => {
setPhase('Upload timed out', 0, false);
showError('Upload timed out after 2 hours. The file may be too large for your connection speed. Try downloading the file directly on the server instead.');
resolve();
});
xhr.addEventListener('load', async () => { xhr.addEventListener('load', async () => {
if (xhr.status === 200) { if (xhr.status === 200) {
const data = JSON.parse(xhr.responseText); const data = JSON.parse(xhr.responseText);