How to return image from $http.get in AngularJS

前端 未结 4 1127
走了就别回头了
走了就别回头了 2020-11-30 04:19

In my controller I call a service that returns a promise

var onComplete = function(data) {
               $scope.myImage = data;           
            };
         


        
4条回答
  •  庸人自扰
    2020-11-30 04:45

    By way of https://stackoverflow.com/a/43032560/418819, you can use "blob" as the responseType and very neatly get the data url with a FileReader.

    $http.get( url, { responseType: "blob" } ).then((result) => {
      var reader = new FileReader();
      reader.readAsDataURL( result.data );
      reader.onload = function (e) {
        return e.target.result;
      };
    });
    

    You can reference it like so:

    
    

提交回复
热议问题