Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display Image received as byte array in Angular JS

I have a server side application that will return an image. These are the response headers:

Content-Disposition: attachment; filename=8822a009-944e-43f4-999b-d297198d302a;1.0_low-res
Content-Length: 502343
Content-Type: image/png
Date: Mon, 03 Aug 2015 19:13:39 GMT
Server: Apache-Coyote/1.1

In angular, I need to display the image. When getting the image, I use the angularJS $http to call the server and put the result in scope, but I never reach the success function of $http. Executing this call from postman returns the image normally. I'm curious to how to get Angular to display the image.

This is how I display the image:

<img ng-src={{image}} />

Here is the call to get the image from the server:

$http.get(url, {responseType: "arraybuffer"})
    .success(function(data) {
        $scope.image= data;
    }
)
like image 798
Loshmeey Avatar asked Aug 03 '15 19:08

Loshmeey


People also ask

How to add images to an angular application?

This tutorial shows multiple examples in Angular, One example is to load images using the img tag Another example is to bind the src attribute to the assets folder. Normally, Images are placed in the src/assets folder in the Angular application.

Why are my images not displaying correctly in angular?

If images do not display correctly, check src/assets folder is included inside the assets attribute in the angular.json file of your angular application. Check path relevant HTML file location with the assets folder

Which tag is used to display an image on an angular?

The Img tag is used to display an image on an angular application. It is declared in the HTML template file. src contains the absolute path of images that referred from the HTML file alt contains string content to display on image for SEO purpose

What is a view in AngularJS?

The View consists of an HTML DIV to which ng-app and ng-controller AngularJS directives have been assigned. Note: If you want to learn more about these directives, please refer my article Introduction to AngularJS. The HTML DIV consists of an HTML FileUpload element and a Button.


2 Answers

Just wanted to add to jdmcnair answer and Loshmeey's comment:

Below is a link to the function that I used to convert the array buffer into a base 64 string.

ArrayBuffer to base64 encoded string

The function:

function _arrayBufferToBase64( 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 );
}

I used this function in my angular controller and then passed the result (using a $scope variable) to the img element in my html file.

like image 102
HarrisonA Avatar answered Oct 06 '22 08:10

HarrisonA


I agree with Bellu's response in that you should be using the .then function, rather than the .success function on the promise returned from the $http.get. However, I'd imagine you'll still have an issue with your ng-src reference in that you are not supplying it with a URL, but instead a reference to your byte array.

To bind your ng-src reference to a byte array held in memory on the client, your binding should take the following form:

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

Edit

Since I never mentioned it explicitly, the ng-src binding above assumes that your image data is in base64 format. HarrisonA provided a method below to convert the array if it isn't already in base64 format.

like image 30
jdmcnair Avatar answered Oct 06 '22 08:10

jdmcnair