将image/jpeg作为arraybuffer或blob返回 [英] returning image/jpeg as arraybuffer or blob
问题描述
我当前正在调用我的api,该api返回一个图像作为image/jpeg
.我的问题是通过javascript angular .factory
资源调用url时,我的数组缓冲区为空的{}
.另外,字节的长度为0.如果我使用响应类型为"或文本"的api url进行调用,则会看到多种类型的值.我在这里想念什么?谢谢您的帮助!
I am currently making a call to my api which returns an image as an image/jpeg
. My issue is the when calling the url through javascript angular .factory
resource I am getting my array buffer as empty {}
. Also, the bytes length is 0. If I make the call to the api url with response type '' or 'text' I do see value of multiple types. What am I missing here? Thank you for your help!
.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 服务只能根据isArray
返回JavaScript对象或数组.要获取诸如 ArrayBuffer 之类的奇异对象,或 Blob ,请使用
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.
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>
这篇关于将image/jpeg作为arraybuffer或blob返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!