返回 image/jpeg 作为 arraybuffer 或 blob
returning image/jpeg as arraybuffer or blob
我目前正在调用我的 api,其中 returns 图像作为 image/jpeg
。我的问题是,当通过 javascript angular .factory
资源调用 url 时,我的数组缓冲区为空 {}
。此外,字节长度为 0。如果我使用响应类型 '' 或 'text' 调用 api url,我会看到多种类型的值。我在这里错过了什么?感谢您的帮助!
JS:
.factory("Img", function($resource) {
return $resource("http://mypathTo/image/:id", {
id: "@id"
}, {
responseType: '' //arraybuffer return empty
});
});
app.controller //代码
$scope.getImage = function(productid) {
console.log(productid);
par = {id: [productid]};
Img.getImage(par).$promise.then(
function(data){
console.log("success:" + data); //I am able to see bytes when coming back as text but not with arraybuffer as data.bytelength = 0
scope.productionPicturePath = data;
return data;
},
function(data){
console.log("error" + data);
}
);
}
}
$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>
我目前正在调用我的 api,其中 returns 图像作为 image/jpeg
。我的问题是,当通过 javascript angular .factory
资源调用 url 时,我的数组缓冲区为空 {}
。此外,字节长度为 0。如果我使用响应类型 '' 或 'text' 调用 api url,我会看到多种类型的值。我在这里错过了什么?感谢您的帮助!
JS:
.factory("Img", function($resource) {
return $resource("http://mypathTo/image/:id", {
id: "@id"
}, {
responseType: '' //arraybuffer return empty
});
});
app.controller //代码
$scope.getImage = function(productid) {
console.log(productid);
par = {id: [productid]};
Img.getImage(par).$promise.then(
function(data){
console.log("success:" + data); //I am able to see bytes when coming back as text but not with arraybuffer as data.bytelength = 0
scope.productionPicturePath = data;
return data;
},
function(data){
console.log("error" + data);
}
);
}
}
$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>