Firebase / Firebase Cloud Storage

Uploading Files to Firebase Cloud Storage

This tutorial will guide you through the process of uploading files to Firebase Cloud Storage from a simple HTML form. Learn how to securely store user-generated content in the cl…

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Explores storing and managing user-generated content such as images and videos using Firebase Cloud Storage.

1. Introduction

Firebase is a comprehensive mobile and web app development platform backed by Google. Firebase Cloud Storage is a powerful, simple, and cost-effective object storage service that allows you to store and serve user-generated content such as photos and videos.

In this tutorial, we will build a simple HTML form to upload files to Firebase Cloud Storage. You will learn how to:

  • Set up Firebase in a web project
  • Upload files to Firebase Cloud Storage
  • Handle upload events

Prerequisites:

  • Basic knowledge of HTML, CSS, and JavaScript
  • A Firebase account
  • A text editor, such as Visual Studio Code

2. Step-by-Step Guide

In the first step, we will create a Firebase project and set up Firebase in our web project. Then, we will create an HTML form for file upload and write JavaScript code to handle file upload events.

2.1 Firebase Project Setup

Go to the Firebase console, click on "Add project", and follow the instructions to create a new project. Once the project is ready, click on "Project settings" and then "Add Firebase to your web app". Copy the config object.

2.2 Firebase in Your Web Project

Include the Firebase libraries in your HTML file. Replace <your-config> with your Firebase project's config object.

<script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-storage.js"></script>

<script>
  var firebaseConfig = <your-config>;
  firebase.initializeApp(firebaseConfig);
</script>

2.3 HTML Form

Create an HTML form for file upload. We will use a simple form with an input field of type file.

<form id="upload-form">
  <input type="file" id="file-field">
  <button type="submit">Upload</button>
</form>

2.4 File Upload

To upload a file, we will listen to the form's submit event, get the file from the input field, and upload it to Firebase Cloud Storage.

document.getElementById('upload-form').addEventListener('submit', function(e) {
  e.preventDefault();

  // Get the file
  var file = document.getElementById('file-field').files[0];

  // Create a storage ref
  var storageRef = firebase.storage().ref('uploads/' + file.name);

  // Upload file
  var task = storageRef.put(file);

  // Update progress bar
  task.on('state_changed', function progress(snapshot) {
    var percentage = snapshot.bytesTransferred / snapshot.totalBytes * 100;
    console.log('Upload is ' + percentage + '% done');
  });
});

3. Code Examples

Let's see two examples with different types of files. Make sure you replace <your-config> with your Firebase project's config object.

3.1 Uploading an Image

<script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-storage.js"></script>

<script>
  var firebaseConfig = <your-config>;
  firebase.initializeApp(firebaseConfig);
</script>

<form id="upload-form">
  <input type="file" id="file-field">
  <button type="submit">Upload</button>
</form>

<script>
document.getElementById('upload-form').addEventListener('submit', function(e) {
  e.preventDefault();

  // Get the file
  var file = document.getElementById('file-field').files[0];

  // Create a storage ref
  var storageRef = firebase.storage().ref('uploads/' + file.name);

  // Upload file
  var task = storageRef.put(file);

  // Update progress bar
  task.on('state_changed', function progress(snapshot) {
    var percentage = snapshot.bytesTransferred / snapshot.totalBytes * 100;
    console.log('Upload is ' + percentage + '% done');
  });
});
</script>

3.2 Uploading a Video

The code to upload a video is the same as the code to upload an image. The only difference is the file you select.

4. Summary

In this tutorial, you learned how to:

  • Set up Firebase in a web project
  • Upload files to Firebase Cloud Storage
  • Handle upload events

As next steps, you could learn how to download files from Firebase Cloud Storage, delete files, and list all files in a folder.

5. Practice Exercises

  1. Modify the code to allow uploading multiple files at once.
  2. Add a progress bar to visually show the upload progress.
  3. Add error handling to the file upload process.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Random String Generator

Generate random alphanumeric strings for API keys or unique IDs.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help