Firebase存储getDownloadURL()不返回工作网址

Leo*_*ame 6 javascript angularjs firebase firebase-storage

我试图使用getDownloadURL()方法来检索存储在firebase存储中的图像的url.

奇怪的是它返回一个url,它是一个对象,而不是图像

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓例子URL↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ↓↓↓↓↓↓↓↓↓↓↓↓ https: /////////////// /说明↑↑↑↑↑↑↑↑↑↑例子URL↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ ↑

我在网上做了一些研究,发现显示我的图像的正确网址应该是这样的......

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓例子URL↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ↓↓↓↓↓↓↓↓↓↓↓↓ https:
//firebasestorage.googleapis.com/v0/b/example.appspot.com/o/images%2Fcat.png? alt = media & token = sometoken↑↑↑↑↑化学网站↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ ↑↑↑↑↑↑↑

但我不知道如何通过使用getDownloadURL()... 获取网址

这是我的代码......

var storage = firebase.storage();

$scope.getImgUrl = function(file) {
    storage.ref("images/" + file + ".png")
      .getDownloadURL()
      .then(function onSuccess(url) {
        return url;
      })
      .catch(function onError(err) {
        console.log("Error occured..." + err);
      })
  }
Run Code Online (Sandbox Code Playgroud)

任何的想法?

Fra*_*len 5

您在then()回调中获得的返回值不会执行您期望的操作。当您打电话时,getDownloadUrl()它会返回一个承诺。如果仅将那个承诺绑定到范围,则可以在HTML中使用它:

$scope.getImgUrl = function(file) {
    return storage.ref("images/" + file + ".png").getDownloadURL();
}
Run Code Online (Sandbox Code Playgroud)


Sub*_*ces 5

因此,从Firebase获取图像URL的正确方法是:

 $scope.getImgUrl = function(file) {
        storage.child("images/" + file +".png").getDownloadURL().then(function(url) {
          return url;
        }).catch(function(error) {
          // Handle any errors here
        });
 }
Run Code Online (Sandbox Code Playgroud)

请注意,您使用的storage.child()不是storage.ref()