Jay Ordway
Jay Ordway

Reputation: 1749

Firebase Storage - Upload Blob Url

I have a blob url such as :

blob:http://localhost:4200/06a6baa9-b7ff-4171-9dc2-a1caed35e099

and when passing into

  this.storage.ref('users/' + uid + '/mainPhoto').put(imageURL))

I am receiving this error:

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

Upvotes: 1

Views: 14048

Answers (2)

chewing gum
chewing gum

Reputation: 105

Referring to dan's solution, it works for me after 2 amendments for Upload Function:-

  1. there should be a 'var' in front of "uploadToStorage = (imageURL)..."
  2. there should not have spacing for '=>'

Upload Function

var uploadToStorage = (imageURL) => {

     getFileBlob(imageURL, blob =>{
        firebase.storage().ref().put(blob).then(function(snapshot) {
           console.log('Uploaded a blob or file!');
        })
    })
}

Upvotes: 4

dan
dan

Reputation: 336

You need to upload the blob from the blob url, and than upload it to firebase storage

Upload Function:

uploadToStorage = (imageURL) = >{

     getFileBlob(imageURL, blob =>{
        firebase.storage().ref().put(blob).then(function(snapshot) {
           console.log('Uploaded a blob or file!');
        })
    })
}

getBlob Function:

var getFileBlob = function (url, cb) {
      var xhr = new XMLHttpRequest();
      xhr.open("GET", url);
      xhr.responseType = "blob";
      xhr.addEventListener('load', function() {
        cb(xhr.response);
      });
      xhr.send();
    };

Upvotes: 9

Related Questions