📖 Scenario: You are building a web app that allows users to upload files to Firebase Storage. You want to show users how much of their file has uploaded in real time, so they know the progress.
🎯 Goal: Create a Firebase Storage upload process that tracks and displays the upload progress percentage.
📋 What You'll Learn
Initialize Firebase Storage reference for a file upload
Create a variable to hold the file to upload
Use the uploadBytesResumable method to start the upload
Add an event listener to track upload progress and update a progress variable
💡 Why This Matters
🌍 Real World
Uploading files with progress feedback is common in web apps like photo sharing, document management, and backups.
💼 Career
Understanding upload progress monitoring is essential for frontend and backend developers working with cloud storage services.
Progress0 / 4 steps
1
Set up the file to upload
Create a variable called file and assign it the value selectedFile which represents the file chosen by the user.
Firebase
Hint
Use const file = selectedFile; to hold the file object.
2
Create a Firebase Storage reference
Create a variable called storageRef and assign it the Firebase Storage reference for the path uploads/${file.name} using ref(storage, `uploads/${file.name}`).
Firebase
Hint
Use ref(storage, `uploads/${file.name}`) to create the storage reference.
3
Start the upload and track progress
Create a variable called uploadTask and assign it the result of uploadBytesResumable(storageRef, file). Then add an event listener on uploadTask for the 'state_changed' event. Inside the listener, calculate the upload progress percentage as (snapshot.bytesTransferred / snapshot.totalBytes) * 100 and assign it to a variable called progress.
Firebase
Hint
Use uploadBytesResumable to start the upload and uploadTask.on('state_changed', callback) to track progress.
4
Complete the upload progress monitoring
Inside the 'state_changed' event listener, add a console log statement that outputs the text Upload is ${progress}% done using a template literal. This will show the upload progress percentage in the console.
Firebase
Hint
Use console.log(`Upload is ${progress}% done`); to show progress.
Practice
(1/5)
1. What does the state_changed event in Firebase Storage upload tasks help you monitor?
easy
A. The number of files in storage
B. The download speed of the file
C. The file size before upload
D. The progress of the file upload
Solution
Step 1: Understand the purpose of state_changed
The state_changed event triggers updates during the upload process.
Step 2: Identify what is monitored
This event provides progress updates, such as bytes transferred and total bytes.
A. The event name should be 'state_changed', not 'state_change'
B. The callback function is missing a return statement
C. You must use uploadTask.addListener instead of on
D. The progress calculation formula is incorrect
Solution
Step 1: Check event name spelling
The correct event name is 'state_changed', not 'state_change'.
Step 2: Understand impact of wrong event name
Using wrong event name means callback never triggers, so no logs appear.
Final Answer:
The event name should be 'state_changed', not 'state_change' -> Option A
Quick Check:
Event name must be exact 'state_changed' [OK]
Hint: Check event spelling: 'state_changed' not 'state_change' [OK]
Common Mistakes:
Misspelling event names
Expecting logs without callback firing
Confusing method names like addListener vs on
5. You want to display upload progress as a percentage and also show a message when upload completes. Which code snippet correctly implements this using Firebase Storage's state_changed event?
hard
A. uploadTask.on('progress', snapshot => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log(`Upload is ${progress}% done`);
});
B. uploadTask.on('state_changed', snapshot => {
console.log('Upload complete!');
});