Upload State Management
You'll learn how to track and persist file upload states so your app always knows where each upload stands, even after a page reload.
What Is Upload State?
Every file upload goes through a lifecycle: it starts pending (selected but not yet sent), moves to uploading (actively transferring), and ends in either success or error. Managing these states means storing which phase each file is in, so you can display accurate progress indicators, retry failed uploads, and prevent duplicate submissions.
Think of it like tracking a pizza delivery: "Order placed" → "Out for delivery" → "Delivered" or "Failed." You need to know the current status at any moment, and if you close the app and reopen it, you still want to see whether your pizza arrived.
Core State Properties
Track these key properties per file:
const uploadState = {
id: 'file-123',
name: 'photo.jpg',
status: 'uploading', // 'pending' | 'uploading' | 'success' | 'error'
progress: 45, // percentage
error: null, // error message if failed
uploadedUrl: null, // server response on success
timestamp: Date.now()
};
Persisting State Across Reloads
For long uploads, store state in localStorage or IndexedDB:
function saveUploadState(fileId, state) {
const allUploads = JSON.parse(localStorage.getItem('uploads') || '{}');
allUploads[fileId] = state;
localStorage.setItem('uploads', JSON.stringify(allUploads));
}
// On page load, restore in-progress uploads
function restoreUploads() {
const saved = JSON.parse(localStorage.getItem('uploads') || '{}');
Object.values(saved).forEach(upload => {
if (upload.status === 'uploading') {
// Resume or retry the upload
}
});
}
Key Takeaway: Upload state management means tracking each file's lifecycle phase (pending, uploading, success, error) and optionally persisting that state so users see accurate status even after page reloads or interruptions.