Batch Upload with Combined Progress
You'll learn how to track and display overall progress across multiple simultaneous file uploads while handling individual failures without stopping the entire batch.
The Core Challenge
When users upload multiple files at once, showing separate progress bars for each file can overwhelm the interface. Instead, you want to display a single, combined progress indicator—like "Uploading 7 of 10 files (63%)"—while ensuring that if file #4 fails, files #5-10 continue uploading normally.
The Combined Progress Formula
Think of your batch as a road trip with multiple cars (files). Even if one car breaks down, the others keep going. Your "trip progress" is the weighted average of all cars' positions.
For each upload, you already have individual progress from XMLHttpRequest.upload.onprogress. To combine them:
function calculateBatchProgress(uploads) {
let totalBytes = 0;
let loadedBytes = 0;
uploads.forEach(upload => {
totalBytes += upload.file.size;
loadedBytes += upload.loaded || 0;
});
return (loadedBytes / totalBytes) * 100;
}
Graceful Error Handling
Each file tracks its own state: pending, uploading, completed, or failed. When one fails, mark it and continue:
upload.catch(error => {
fileState.status = 'failed';
fileState.error = error.message;
// Batch continues with remaining files
updateCombinedProgress();
});
Your UI can show: "8 of 10 complete (2 failed)" with options to retry failed uploads individually.
The Orchestration
Update combined progress whenever any file's progress changes, recalculating across all active uploads. Use a shared state object that all upload promises reference, allowing real-time aggregation without blocking.
Key Takeaway: Combined batch progress requires summing individual loaded/total bytes across all uploads, displaying aggregate metrics, and isolating failures so successful uploads complete independently.