Firebase Storage - Upload Blob Url

柔情痞子 提交于 2019-12-11 17:03:25

问题


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.


回答1:


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();
    };



回答2:


You don't need to pass the blob's url, just pass the blob itself.

var file = ... // use the Blob or File API
ref.put(file).then(function(snapshot) {
    console.log('Uploaded a blob or file!');
});

See the 'Upload from a Blob or File' section at Firebase File Upload.



来源:https://stackoverflow.com/questions/48862777/firebase-storage-upload-blob-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!