How to retrieve image's url from Firebase's storage?

落花浮王杯 提交于 2019-12-25 08:33:04

问题


I have this code straight from the documentation of Firebase :

// Create a reference to the file we want to download
  var user = firebase.auth().currentUser.uid;
  var storageRef = firebase.storage();
  var pathReference = storageRef.ref('/' + user + '/profilePicture/' + image.name);

  //var starsRef = storageRef.child('/' + user + '/profilePicture/' + file.name);

  // Get the download URL
  pathReference.getDownloadURL().then(function(url) {
  // Insert url into an <img> tag to "download"
      $scope.imageUrl = url;
      console.log($scope.imageUrl);
  });

But I can not figure what to write in my html file in order to get the image showing... Any help ?


回答1:


Just put your result inside a variable of your controller and in ng-src on the HTML.

The best would be to use a service to get the URL and not directly in the controller.

Controller

app.controller('MyController', ['$scope', function($scope) {
    // Create a reference to the file we want to download
    var user = firebase.auth().currentUser.uid;
    var starsRef = storageRef.child('/' + user + '/profilePicture/' + file.name);

    // Get the download URL
    starsRef.getDownloadURL().then(function(url) {
    // Insert url into an <img> tag to "download"
        $scope.imageUrl = url;
    });
}]);

HTML

<img ng-src="{{imageUrl}}"/>


来源:https://stackoverflow.com/questions/42228193/how-to-retrieve-images-url-from-firebases-storage

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