angularJS display base64 image

If the image is from remote server, you can fetch the base64 content in your controller like this:

$http.get($scope.user.photo).then(function(response) {
    $scope.user.data = response.data;
});

then display it on view

<img data-ng-src="data:image/png;base64,{{user.data}}" data-err-src="images/png/avatar.png"/>

While if the image is on local disk/phone, first you can get the image data from FileReader

var fileInput = document.getElementById("myfileinput");

// files is a FileList object (similar to NodeList)
var files = fileInput.files;
$scope.imageSrc = files[0].getAsDataURL();

then bind the $scope.imageSrc to view as <img ng-src="https://stackoverflow.com/questions/28235246/imageSrc" >

Leave a Comment