returning image/jpeg as arraybuffer or blob

五迷三道 提交于 2019-12-02 11:49:38

The $resource service can only return JavaScript objects or arrays depending on isArray. To get exotic objects such as ArrayBuffer or Blob, use the $http service.

The DEMO

angular.module("app",[])
.controller("ctrl", function($scope,$http) {
    var vm = $scope;
    var url="//i.imgur.com/fHyEMsl.jpg";

    var config = { responseType: 'blob' };
    $http.get(url,config)
      .then(function(response) {
        console.log("OK");
        //console.log(response.data);
        vm.blob = response.data;
        vm.dataURL = URL.createObjectURL(vm.blob);
        console.log(vm.dataURL);
    }).catch(function(response) {
        console.log("ERROR");
        throw response;
    });
})
<script src="//unpkg.com/angular/angular.js"></script>
  <body ng-app="app" ng-controller="ctrl">
      BLOB type {{blob.type}}<br>
      BLOB size {{blob.size}}<br>
    <img ng-src="{{dataURL}}" height="100" />
  </body>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!