如何读取在angularjs二进制数据 [英] how to read a binary data in angularjs
问题描述
我已经写了一个API服务,从$ HTTP获取一个二进制数据。问题是我不知道如何使用这些数据来显示图像。 PS:二进制数据实际上是一个图像。
I've written an api service which fetch a binary data from $http. The problem is I don't know how to used that data to display the image. PS: the binary data is actually an image.
这是我的服务API
getAvatar: function() {
return $http({
method : 'GET',
url : 'domain.com',
headers : {'Content-Type' : 'application/json'}
});
}
我的控制器
//get avatar
$scope.userAvatar = function() {
Api.getAvatar()
.then(function(result) {
//success
console.log(result.data); //this is the binary data
$scope.avatarImage = result.data;
}
}, function(result) {
//errors
});
};
起初我以为结果是API会给我一个网址,但事实证明,一个二进制。我必须先转换?
At first I thought that result that the api will give me is a url but it turns out a binary. Do I have to convert it first?
推荐答案
我假设你的服务可能会返回的base64连接$数据C $ CD字符串的任何图像,你再绑定到 avatarImage
在您的控制器。
I assume your service may be returning the base64 encoded string of data for any image which you then bind it to avatarImage
in your controller.
然后,只需在HTML中写:
Then simply write in html:
< IMG NG-SRC ={{avatarImage}}/>
这篇关于如何读取在angularjs二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!