1

I want to display image on the page that is returned by the java controller.

Java controller:

 public FileSystemResource RetrieveLogo(@PathVariable("UniqueCode") String Code){

// get image file

return fileSystemResource;
} 

at java script controller, I make a service call to get the image .

Javascript controller:

$scope.logo = function() {
         rest.retrieveLogoForMerchant({id: 'abcd'}).$promise.then(function(data) {
                            console.log("-------------success--------- " + angular.toJson(data));

                            $scope.logoImage = angular.toJson(data);
                        });
    }

Here the console prints -

{"0":"�","1":"�","2":"�","3":"�","4":"\u00.......etc}

On Html page

<img ng-src="{{logoImage}}" height="100" width="100"/>

On the page image displays blank and gives error in the console that -

http://localhost:9090/%7B%220%22:%22%EF%BF%BD%22,%221%22:%22%EF%BF%BD%22,%2…22%EF%BF%BD%22,%22246%22:%22\u0007%22,%22247%22:%22\u0014%22,%22248%22:%22 400 (Bad Request)

Suggest me ,that how can I display image properly.

Thanks

3 Answers 3

3

Try removing the the angular.toJSON method here:

$scope.logoImage = angular.toJson(data);

And also

I think you are loading image through binary data append proper image encoding to the data Eg: ng-source={{data:image/png;base64........}}

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried removing angular.toJson. if i'll remove that then other angular resource parameter will be added ($promise.$resolve). Thats, why I have added that.
i am assuming data parameter in the success callback is binary data streamed from server. Can you paste down the service/factory logic of the method retrieveLogoForMerchant of rest.
1

You need to set the right content type in response inside your Java controller, see below function

public void displayImage(HttpServletResponse response, byte[] imageArray) throws ServletException, IOException {
        System.out.println("Inside display Image function");

        if (imageArray != null && imageArray.length > 0) {
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            System.out.println("Image Array is not null :" + imageArray);
            response.setContentType("image/jpeg");
            OutputStream out = response.getOutputStream();
            out.write(imageArray);
            out.flush();
            out.close();
        } else {
           System.out.println("Image is null");
        }
    }

Than you need to make sure append that appropriate header before data content while setting it to img tag.. @Naveen has already pointed about it in his answer. Basically you need to have something like this

<img src="data:image/png;base64,/*Your data part goes here*//>

You also need to encode the stream to Base64 before assigning it to img. I had below line in my jsp you can alter it to suit your need..

<img id="userProfileImg" height="150px" width="150px"  name="userProfileImg" alt="../img/user_default_pic.png" src='data:image/jpg;base64,<%=Base64.encode(/*Blob data returned from database*/)%>'/>

Comments

0

Why don't you get the URL of the image via http (ajax call) and on success $scope.logoImage = data. data is the URL of the image retrieved via http.

3 Comments

I didn't get your point .Can you elaborate? I am doing service call and on the success of that i am assigning data to $scope.logoImage.
I think you are returning the whole image-data from the server on the service call request. Whereas image-url should be assigned to ng-src. So, your server should return just the URL (string) of the image, not the image itself, on service request.
Also it should be ng-src='logoImage' instead of ng-src='{{logoImage}}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.