How to return image from $http.get in AngularJS

前端 未结 4 1110
走了就别回头了
走了就别回头了 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:33

    None of the methods seems to be complete, this is a complete solution:

      $http({
        method: 'GET',
        url: imageUrl,
        responseType: 'arraybuffer'
      }).then(function(response) {
        console.log(response);
        var str = _arrayBufferToBase64(response.data);
        console.log(str);
        // str is base64 encoded.
      }, function(response) {
        console.error('error in getting static img.');
      });
    
    
      function _arrayBufferToBase64(buffer) {
        var binary = '';
        var bytes = new Uint8Array(buffer);
        var len = bytes.byteLength;
        for (var i = 0; i < len; i++) {
          binary += String.fromCharCode(bytes[i]);
        }
        return window.btoa(binary);
      }
    

    Then I am able to use it directly:

    <img data-ng-src="data:image/png;base64,{{img}}">
    

    The function to convert arraybuffer into base64 is directly taken from ArrayBuffer to base64 encoded string

    0 讨论(0)
  • 2020-11-30 04:33

    Just in case anyone needs it.

    In my case, I had to send the request through angular's $http service, because of various transformers and other fancy stuff we do with it.

    So based on the Mozilla's guide mentioned earlier, I came up with the following solution:

    let getImageDataURL = (url, imageType = 'image/jpeg') => {
      return $http.get(url, {responseType: 'arraybuffer'}).then((res) => {
        let blob = new Blob([res.data], {type: imageType});
        return (window.URL || window.webkitURL).createObjectURL(blob);
      });
    };
    

    The basic idea is to set the responseType property of the underlying XHR request and the convert the binary content to data URL.

    0 讨论(0)
  • 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:

    <img data-ng-src="{{img}}">
    
    0 讨论(0)
  • 2020-11-30 04:58

    The image that's coming back is in binary encoding, rather than Base64.

    Understandably, <img> tags don't support sourcing from binary through attributes, so you'll have to look at another solution.

    You could try converting the binary encoding to Base64 at the client side using TypedArrays together with the btoa function. Then you'd be able to use

    <img ng-src="data:image/JPEG;base64,{{myImage}}">
    

    This guide a by Mozilla covers making an XHR request for and image and reading it directly into a UInt8Array. It should be a good starting place.

    It's written for plain old Javascript, but translating it to Angular should be a good exercise if you are just learning the ropes.

    0 讨论(0)
提交回复
热议问题