首页 文章

Firebase存储 - 上传Blob网址

提问于
浏览
0

我有一个blob网址,例如:

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

而当进入

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

我收到此错误:

Firebase存储:索引0处的put中的参数无效:预期的Blob或文件 .

2 回答

  • 0

    您需要从blob网址上传blob,然后将其上传到firebase存储

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

    你不需要传递blob的url,只需传递blob本身 .

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

    请参阅Firebase File Upload处的'Upload from a Blob or File'部分 .

相关问题