How to upload an image using HTML and JavaScript in firebase ?
Last Updated :
26 Jul, 2024
Firebase is a product of Google which helps developers to build, manage, and grow their apps easily. It helps developers to build their apps faster and in a more secure way. No programming is required on the firebase side which makes it easy to use its features more efficiently. It provides cloud storage. It uses NoSQL for the storage of data.
Here, We are going to learn How we can upload images using HTML and JavaScript in firebase. While working with the database we may require to upload an image file also.
Step By Step Implementation
Step 1: If you are new to firebase then please refer to this.
Activate Firebase Storage:
Click on the Storage button on the left and click on Get Started.
After that this box will pop up. Click on Next.
Then click on Done.
Step 2: Here we're going to write the Html and JavaScript Code to connect Html Code with Firebase Database.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Collecting Data</title>
<link rel="stylesheet"
href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity=
"sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
crossorigin="anonymous">
</head>
<body class="container"
style="margin-top: 50px;
width: 50% height:auto; ">
<h2 class="text-primary"
style="margin-left: 15px; margin-bottom: 10px">
Hey There,Here we are going to upload
</h2>
<form class="container" id="contactForm">
<div class="card">
<div class="card-body">
<div class="form-group"
style="margin-left: 15px;
margin-top: 10px;
display:none;>
<label for=" exampleFormControlSelect1 ">Select Type</label>
<select class="form-control " id="types ">
<option>1</option>
</select>
</div>
<br>
Document Upload:
<br>
<!-- click here to choose file -->
<input type="file " name="files[] " id="files ">
<!-- click here to upload file -->
<input type="hidden "
name="url "
id="url ">
<button type="button " onclick="uploadimage() ">
Upload Image
</button>
<br><br>
</div>
</div>
<button type="submit "
class="btn btn-primary "
style="margin-left: 15px; margin-top: 10px; display:none; ">
Submit
</button>
</form>
</body>
<script src="https://www.gstatic.com/firebasejs/3.7.4/firebase.js ">
</script>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js ">
</script>
<link type="text/css " rel="stylesheet " href=
"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css " />
<script src=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js ">
</script>
<script>
// adding firebase data
var firebaseConfig = {
apiKey: "*********************- ",
authDomain: "-********************* ",
databaseURL: "********************* ",
projectId: "********************* ",
storageBucket: "********************* ",
messagingSenderId: "********************* ",
appId: "********************* "
};
firebase.initializeApp(firebaseConfig);
var messagesRef = firebase.database().ref('Checking');
document.getElementById(
'contactForm').addEventListener('submit', submitForm);
//uploading file in storage
function uploadimage(){
var type = getInputVal('types');
var storage = firebase.storage();
var file=document.getElementById("files ").files[0];
var storageref=storage.ref();
var thisref=storageref.child(type).child(file.name).put(file);
thisref.on('state_changed',function(snapshot) {
}, function(error) {
}, function() {
// Uploaded completed successfully, now we can get the download URL
thisref.snapshot.ref.getDownloadURL().then(function(downloadURL) {
//getting url of image
document.getElementById("url ").value=downloadURL;
alert('uploaded successfully');
saveMessage(downloadURL);
});
});
// Get values
var url = getInputVal('url');
// Save message
// saveMessage(url);
}
function getInputVal(id){
document.getElementById('contactForm').reset();
}
// Function to get form values
function getInputVal(id){
return document.getElementById(id).value;
}
// Save message to firebase database
function saveMessage(url){
var newMessageRef = messagesRef.push();
newMessageRef.set({
imageurl:url,
});
}
</script>
</html>
After you have written the code you can run it using the below command in the terminal
python manage.py runserver
Here, We will see the following screen after running it on any web browser.
After we select an image using choose file button we can choose any file, and then we will click on upload Image button to upload the image in firebase
In the Real-time database, we can see the image is uploaded successfully.

Similar Reads
How to Upload Files in JavaScript? We upload files using JavaScript, for this we have to capture form elements, gather information from FormData for easier file uploads, intercept submission events, and utilize Fetch API for asynchronous server requests, for enhanced user experience and efficient data handling. ApproachProject Setup:
1 min read
How to Create an Image Element using JavaScript? Creating an image element dynamically using JavaScript is a useful technique when you want to add images to a webpage without having to manually write the <img> tag in your HTML. This allows for more flexibility, as we can create and manipulate images based on user interactions or other condit
2 min read
How to Add Images in RecyclerView using Firebase? It is a flexible Android view group that is basically used to display a large set of information in a scrollable manner. It is created to display large sets of data by reusing views as the user scrolls through the list, which provides smooth scrolling and less memory usage. It supports animations, i
5 min read
How to Upload Image and Preview it Using ReactJS? In React, uploading and previewing images improve the user experience of the application. It's typical for online apps to provide image upload capability that enables users to choose and upload photographs. We simply upload a photo from our local device to our React Project and preview it using a st
2 min read
How to convert image into base64 string using JavaScript ? In this article, we will convert an image into a base64 string using Javascript. The below approaches show the methods to convert an image into a base64 string using Javascript.ApproachHere we will create a gfg.js file which will include JavaScript code and one gfg.html file.Now we will put onchange
2 min read
Create a website using HTML CSS and JavaScript that stores data in Firebase Following are some simple steps in order to connect our static Web Page with Firebase. Step-by-Step ImplementationStep 1: Firstly, We are going to create a project on Firebase to connect our static web page. Visit the Firebase page to configure your project. Visit the website and click the On Add P
3 min read
How to Upload a Video in Firebase Database using Android Studio? Firebase is a mobile and web application development platform. It provides services that a web application or mobile application might require. Firebase provides secure file uploads and downloads for the Firebase application. This article explains how to build an Android application with the ability
3 min read
How to upload files in firebase storage using ReactJS ? Firebase Storage is a powerful cloud storage solution provided by Google's Firebase platform. It allows developers to store and retrieve user-generated content, such as images, videos, and other files, in a secure and scalable manner. In this article, we will explore how to upload files to Firebase
2 min read
Android - Upload an Image on Firebase Storage with Kotlin Firebase Cloud Storage stores files in the Google Cloud Storage bucket and makes them accessible through both Firebase and Google Cloud. It allows us to upload and download files from mobile clients through the Firebase SDKs, and perform server-side processing such as image filtering or video transc
6 min read
How to Compress Image in Android Before Uploading it to Firebase Storage? Usually, in an app we make users upload images like profile images or others. But there is a chance that the uploaded image is only for display purposes, that too in a short frame. So, these images do not have to be stored in a high-resolution format. Hence even if the user uploads a high-resolution
4 min read