【问题标题】:firebase snapshot.downloadURL is undefined after successfully uploading an image成功上传图片后,firebase snapshot.downloadURL 未定义
【发布时间】:2020-02-09 02:31:00
【问题描述】:

我的应用程序中有这段代码,我用它来上传图像并获取它的 url,以便我可以将其保存在数据库中,图像是 base64 格式并且上传成功,正如我在 @987654322 中看到的那样@输出,并且通过检查我的firebase存储但是,快照的downloadUrl属性是未定义的,我不知道为什么。这不是它应该工作的方式

storage.$putString(b64, 'data_url', {contentType:'image/jpg'}).$complete(function(snapshot) {
        console.log(snapshot);
        item.avatarUrl=snapshot.downloadURL;
        agents.$add(item).then(function(ref) {
        });

    });     

【问题讨论】:

标签: firebase firebase-storage angularfire snapshot


【解决方案1】:

使用snapshot.ref.getDownloadURL()

【讨论】:

  • 现在返回一个承诺。请参阅最低点的答案。
【解决方案2】:

November 2019 更新来自 firebase 的官方文档:

function(){
      uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
        console.log('File available at', downloadURL);
      });

    });

Here is the source from firebase documentation

【讨论】:

    【解决方案3】:

    我正在使用 angularfire2 5.0.0-rc11 并面临同样的问题。我修改代码如下:

    storage.$putString(b64, 'data_url', {contentType:'image/jpg'}).$complete(function(snapshot) {
        console.log(snapshot);
        //item.avatarUrl=snapshot.downloadURL;
          // changed to:
          snapshot.getDownloadURL()
          .then( downloadUrl => {
    
            item.avatarUrl=downloadUrl
            agents.$add(item).then(function(ref) {
            });
          })
          .catch( error => {
            console.log(error);
            //catch error here
          });
    
    });    
    

    【讨论】:

      【解决方案4】:

      如果您使用的是 Andorid,请使用此代码获取上传的文件 Uri。

      Task<Uri> downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl(); 
      Uri fileUri = downloadUrl.getResult().toString();
      

      【讨论】:

        猜你喜欢
        • 2018-02-10
        • 2023-02-08
        • 1970-01-01
        • 2012-10-07
        • 2017-12-15
        • 1970-01-01
        • 1970-01-01
        • 2019-09-28
        • 2021-04-12
        相关资源
        最近更新 更多