Anj*_*hra 3 javascript base64 bytearray angularjs cordova
我在服务响应中获取字节数组,该图像将显示在我的html页面的图像字段中.任何想法我怎么能实现这一点.我试图找出这个堆栈溢出的解决方案,但无法获得有效的解决方案.请帮忙.我的代码是:
this.getPrescription = function(pres_id) {
var deff = $q.defer();
$http({
method: "GET",
url: "www.abc.com/api/&prescriptionOnly=false&page=1",
headers: {
'Authorization': 'Bearer ' + localStorage.getItem("chemist_access_token"),
'Content-Type': 'application/json'
},
responseType: 'arraybuffer'
}).then(function(objS) {
console.log("getPrescription:\n" + JSON.stringify(objS))
deff.resolve(objS);
}, function(objE) {
errorHandler.serverErrorhandler(objE);
deff.reject(objE);
});
return deff.promise;
};
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我打电话给:
$scope.getPrescription = function(id) {
$ionicLoading.show({
template: '<ion-spinner icon="spiral"></ion-spinner>',
noBackdrop: false
});
serverRepo.prescriptionGet(id).then(function(objS) {
console.log("orderByCustomer:\n" + JSON.stringify(objS));
$scope.picdata=$window.URL.createObjectURL(new Blob([objS.data], {type: 'image/png'}));
$ionicLoading.hide();
console.log("getOrderByNew_success_loadMore:\n" +$scope.picdata);
}, function(objE) {
$ionicLoading.hide();
});
}
Run Code Online (Sandbox Code Playgroud)
当我检查我的控制台时,它显示:getOrderByNew_success_loadMore:blob:file:/// 0aa86d9f-61a1-4049-b18c-7bf81e05909f
小智 7
使用此过滤器将字节数组转换为base64
app.filter('bytetobase', function () {
return function (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);
};
});
Run Code Online (Sandbox Code Playgroud)
将其绑定为图像使用
<img ng-src="data:image/JPEG;base64,{{picture | bytetobase}}" alt="..." width="100" height="100">
Run Code Online (Sandbox Code Playgroud)
或者,如果您需要为其分配变量使用
var image = $filter('bytetobase')($scope.picture );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12841 次 |
| 最近记录: |