Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Show byte array content as image

I'm searching into my database a image as a byte array. I want to show this content as file using the markup image, but it doesn't work here.

// Controller which get my image and put into $scope.
function MyController($scope, $http, $location) {  

    var url = '/json/FindImage?id=1';  
    $http.get(url).success(function(result) {  
        $scope.image = result.Image;  
    }  
}  

// HTML
<!-- It doesn't work -->  
<img src="{{image}}" />  
<!-- It doesn't work also -->  
<img ng-src="{{image}}" />  

Any idea? Thank you all!

like image 362
Kiwanax Avatar asked Feb 20 '13 12:02

Kiwanax


2 Answers

Use ng-src in the following format

<img ng-src="data:image/JPEG;base64,{{image}}">

Don't forget to add a sanitization filter for data to not be marked as unsafe by angular:

$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|file|ftp|blob):|data:image\//);
like image 115
Assem-Hafez Avatar answered Sep 24 '22 03:09

Assem-Hafez


If you can get your server to return the image in a base64 encoded string, you could use a data url as the src attribute.

like image 40
Anders Ekdahl Avatar answered Sep 20 '22 03:09

Anders Ekdahl