【问题标题】:Firebase Storage - Upload Blob UrlFirebase 存储 - 上传 Blob 网址
【发布时间】:2018-07-29 11:42:51
【问题描述】:

我有一个 blob url,例如:

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

当传入

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

我收到此错误:

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

【问题讨论】:

    标签: firebase blob firebase-storage


    【解决方案1】:

    您需要从 blob url 上传 blob, 然后将其上传到 Firebase 存储

    上传功能

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

    getBlob 函数

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

    【讨论】:

    • 这是使用 expo 时的最佳解决方案。由于某种原因,现在 fetch 不起作用..
    • getFileBlob 是从哪里来的?
    【解决方案2】:

    参考dan的解决方案,对上传功能进行了2次修改后对我有用:-

    1. “uploadToStorage = (imageURL)...”前面应该有一个“var”
    2. '=>' 不应有空格

    上传功能

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

    【讨论】:

      猜你喜欢
      • 2020-05-14
      • 2019-04-20
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      • 2014-08-23
      • 2018-05-30
      相关资源
      最近更新 更多