Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload a base64 image with Firebase Storage

I'm making this app where users can have a profile picture (but only one picture each). I got everything set up, but when the pictures are 2mb+, it takes some time to load and actually I just need the pictures to be 50kb or so (only small display of pictures, max 40 pixels). I made some code to put the images directly into the realtime database (convert to canvas and make them a 7kb base64 string). However, this is not really clean and it's better to use Firebase Storage.

Since the new update 3.3.0 you can upload Base64 formatted strings to Storage, using the putString() method. However, when I upload my canvas image (which starts with "data:image/jpeg;base64,"), I get the error:

v {code: "storage/invalid-format", message: "Firebase Storage: String does not match format 'base64': Invalid character found", serverResponse: null, name: "FirebaseError"}.

Does this error occur because of string of the canvas image in the beginning? I've searched all over Stack, but I can't seem to find the answer.

like image 378
Zizazorro Avatar asked Sep 03 '16 18:09

Zizazorro


People also ask

How do I upload images to Firebase Storage?

Create a new project on android studio or open an existing project in which you want to add authentication and add the firebase to that android application. Two buttons: one for selecting an image from gallery. other button is for uploading an image on firebase storage on the cloud.

How do I insert an image into base64?

Use base64 as CSS background You can use the base64 encoded string as a CSS background image, too. Simply feed the url() parameter with data:image/... You can obtain the example code by pressing the copy css button and it will be copied to your clipboard.

Which method can we use to upload an image to Firebase Cloud Storage?

The putStream() method is the most versatile way to upload a file to Cloud Storage. putStream() takes an InputStream and returns an UploadTask that you can use to manage and monitor the status of the upload. // taskSnapshot. getMetadata() contains file metadata such as size, content-type, etc.


1 Answers

Jeez, I've been busy for a very long time now, but just after I posted this, I've found the answer myself. The solution was to get the base64 variable and remove the first 23 digits (so: "data:image/jpeg;base64,") and upload it to the Firebase Storage. Now it gets accepted and you can put the link in your Realtime Database via:

var storageRef = firebase.storage().ref().child("Whatever your path is in Firebase Storage");
var imageRef = "Your path in the Realtime Database";

    storageRef.getDownloadURL().then(function(url) {
        imageRef.child("image").set(url);
    }); 

    var task = storageRef.putString("Your base64 string substring variable", 'base64').then(function(snapshot) {
         console.log('Uploaded a base64 string!');
         });
like image 113
Zizazorro Avatar answered Sep 22 '22 15:09

Zizazorro