/firebase-tutorials

How to upload files to Firebase storage?

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.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to upload files to Firebase storage?

 

Step 1: Set Up a Firebase Project

 

  • Go to the Firebase Console.
  • Click on "Add project" and enter your project's details.
  • Once your project is created, navigate to the project dashboard.

 

Step 2: Add Firebase to Your App

 

  • Click on the web icon (</>) to register your web app.
  • Enter the app's nickname and register it.
  • Firebase will provide you with a configuration snippet. Keep this snippet as you will need it for your HTML file.

 

Step 3: Set Up Firebase Storage

 

  • In the Firebase Console, go to "Build" in the left sidebar, then select "Storage".
  • Click "Get started" and choose the security rules according to your needs, either "Production mode" or "Test mode".
  • Click "Next" and then "Done".

 

Step 4: Install Firebase SDK

 

  • For a web application, add the following <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

 

  • Add the Firebase configuration details you got in Step 2 to your 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

 

  • Create a basic HTML form for users to select a file to upload.

<input type="file" id="fileInput" />
<button id="uploadButton">Upload</button>

 

Step 7: Handle File Upload in JavaScript

 

  • Add an event listener to handle the upload functionality in your JS file or within a <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

 

  • Go to the Firebase Console, click on "Storage" and navigate to "Rules". Update the rules to allow reading and writing if necessary.

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=\*\*} {
      // Allow read/write to all files
      allow read, write: if true;
    }
  }
}
  • Warning: These rules allow read/write access to your storage from anyone. Modify them according to your security requirements before deploying.

 

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.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022