Learn how to upload files to Firebase Storage with our concise guide. Set up your Firebase project, integrate the SDK, and securely handle file uploads.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up a Firebase Project
Step 2: Add Firebase to Your App
Step 3: Set Up Firebase Storage
Step 4: Install Firebase SDK
<script> tags to your index.html file, which includes Firebase SDKs.
<script src="https://www.gstatic.com/firebasejs/9.16.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.16.0/firebase-storage.js"></script>
Step 5: Initialize Firebase in Your App
index.html.
<script>
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_SENDER_ID",
appId: "YOUR_APP_ID"
};
// Initialize Firebase
const app = firebase.initializeApp(firebaseConfig);
</script>
Step 6: Prepare the HTML for File Upload
<input type="file" id="fileInput" />
<button id="uploadButton">Upload</button>
Step 7: Handle File Upload in JavaScript
<script> tag.
<script>
const storage = firebase.storage();
const fileInput = document.getElementById('fileInput');
const uploadButton = document.getElementById('uploadButton');
uploadButton.addEventListener('click', () => {
const file = fileInput.files[0];
if (!file) {
console.error('No file selected for upload.');
return;
}
const storageRef = storage.ref(`uploads/${file.name}`);
const uploadTask = storageRef.put(file);
uploadTask.on('state\_changed',
(snapshot) => {
// Progress function
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) \* 100;
console.log('Upload is ' + progress + '% done');
},
(error) => {
// Error function
console.error('Upload failed:', error);
},
() => {
// Complete function
uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => {
console.log('File available at', downloadURL);
});
}
);
});
</script>
Step 8: Set Firebase Storage Rules
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=\*\*} {
// Allow read/write to all files
allow read, write: if true;
}
}
}
Conclusion
You have now successfully set up Firebase Storage and implemented a basic file upload feature in your web application. This tutorial covered setting up Firebase, configuring Firebase Storage, preparing the HTML interface, and handling file uploads using JavaScript.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.