Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save canvas as image to Firebase storage?

I'm trying to save canvas as an image to the firebase storage. I have read many articles and questions about saving canvas to server, and tried to implement the same with the below code.

function server(){
    canvas = document.getElementById("c");
    var storageRef = firebase.storage().ref();
    var mountainsRef = storageRef.child('mountains.jpg');
    var image = new Image();
    image.src = canvas.toDataURL("image/png");
    var uploadTask = storageRef.child('images/' + "apple").put(image);
    uploadTask.on('state_changed', function(snapshot){
        // Observe state change events such as progress, pause, and resume
        // See below for more detail
    }, function(error) {
        // Handle unsuccessful uploads
    }, function() {
        // Handle successful uploads on complete
        // For instance, get the download URL: https://firebasestorage.googleapis.com/...
        var downloadURL = uploadTask.snapshot.downloadURL;
    });
}

But when I run the web app, the console shows error:

FirebaseError: Firebase Storage: Invalid argument in put at index 0: Expected Blob or File.

How can I successfully save a canvas to Firebase storage?

like image 347
pavitran Avatar asked Jun 17 '16 05:06

pavitran


People also ask

How do I upload images to Firebase from Node JS?

Uploading Image to the Firebase Storage using Cloud functions from NodeJS Environment. If you are familiar with firebase, you will agree that file upload is usually done on the client-side using the Firebase SDK. You first upload files to firebase storage, download the URL and send it to the Firestore.

How do I upload files to Firebase?

If you are familiar with firebase, you will agree that file upload is usually done on the client-side using the Firebase SDK. You first upload files to firebase storage, download the URL and send it to the Firestore.

How does cloud storage for Firebase save bandwidth?

If the network connection is poor, the client is able to retry the operation right where it left off, saving your users time and bandwidth. Cloud Storage for Firebase stores your files in a Google Cloud Storage bucket, making them accessible through both Firebase and Google Cloud.

What is add file metadata cloud storage for Firebase?

Add File Metadata Cloud Storage for Firebase allows you to quickly and easily upload files to a Cloud Storage bucket provided and managed by Firebase. Note: By default, a Cloud Storage bucket requires Firebase Authentication to perform any action on the bucket's data or files.


1 Answers

Yes this is possible. The problem you are having is that you are trying to upload a dataUrl but firebase's put function only excepts blobs or files. To convert a canvas to a blob use the toBlob function.

canvas.toBlob(function(blob){
  var image = new Image();
  image.src = blob;
  var uploadTask = storageRef.child('images/' + "apple").put(blob);
}); 

Edit: changed var uploadTask = storageRef.child('images/' + "apple").put(image); to var uploadTask = storageRef.child('images/' + "apple").put(blob);

Also not sure if this will work in your case when i tried it I got a tainted canvas error.

What worked for me was the answer to this question How to convert dataURL to file object in javascript?

like image 61
alecschrader Avatar answered Sep 20 '22 20:09

alecschrader