后端blob 怎么显示在页面_使用angularjs在HTML中显示Blob图片

I have some images stored as blob in mysql database. I want to display it in html. I get the response from the server like this:

{"data":[{"ClassImage":{"type":"Buffer","data":[91,111,98,106,101,99,116,32,70,105,108,101,93]}}],"status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"http://192.168.1.19:80/getImage","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"OK"}

I don't really know what I'm doing. AFter searching I found that I have to convert the blob response to base64 and then create a temporary url for the image and use it to set the src for the image.

When I set the responseType to blob, the data part of the response is empty({}). When I don't set any response type I get it as [91,111,98,106,101,99,116,32,70,105,108,101,93].

This is the code:

$scope.getImage = function(){

$http({

method:'GET',

url: $scope.ipForHttp+"getImage"

// responseType:'arrayBuffer'

})

.then(function(response){

var u8 = new Uint8Array(response.data[0].ClassImage.data);

var b64encoded = btoa(String.fromCharCode.apply(null, u8));

var file = new Blob(u8, {type: 'image/png'});

$scope.fileURL = URL.createObjectURL(file);

$scope.content = $sce.trustAsResourceUrl($scope.fileURL);

console.log($scope.fileURL);

console.log(JSON.stringify(response));

})

}

The html:

Image:

{{fileURL}}

The fileURL I get after the createObjectURL isblob:http://192.168.1.19/6d5ab92f-7e86-4537-893c-f22826ed1b5a

When I do this I don't get the image. When I go to the url the page is blank. What am I doing wrong?

解决方案

I just had to convert the response to base64 and display it.

$scope.getImage = function(){

$http({

method:'GET',

url: $scope.ipForHttp+"getImage"

// responseType:'arrayBuffer'

})

.then(function(response){

$scope.b64encoded = btoa(String.fromCharCode.apply(null, response.data[0].ClassImage.data));

})

}

And in html:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值