0

Hi I'm still new with angularjs and spring mvc, My server is working fine When I test it on its own I can see the image but in the client side it's not working and I'm having this error in my console angular.js:3153 GET data:image/JPEG;base64,{} net::ERR_INVALID_URL any help please.

ProductController.java

@RequestMapping(value = "/getImg/{id}", method = RequestMethod.GET)
    @ResponseBody
    public void getImg(@PathVariable long id, HttpServletResponse response) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        response.setHeader("Content-Disposition", "inline; filename=productImg.png");
        Model model = modelRepo.findById(id);
        model.getUrlImg();
        FileInputStream in = new FileInputStream(model.getUrlImg());
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) != -1) {
            response.getOutputStream().write(buffer, 0, len);
        }

        response.setContentType("image");

    }

myService.js

getImg : function(id)
                        {
                            return $http
                            .get(
                                    'http://127.0.0.1:9001/PrototypePos/rest/products/getImg/'+id,
                                    {responseType : 'arraybuffer'})
                            .then(
                                    function(response) {
                                        return response.data;
                                    },
                                    function(errResponse) {
                                        console
                                                .error('Error while fetching clients');
                                        return $q.reject(errResponse);
                                    });
                        },

myController.js

$scope.getImg = function(id,$compileProvider) {
    ProductService.getImg(id).then(function(data) {
        $scope.image=data;
console.log($scope.image +"***************");



    }, function(errResponse) {
        console.error('Error while fetching allPdts');
    });
};

In my html code:

<img ng-src="data:image/JPEG;base64,{{image}}">
10
  • Why you save image as data in DB?, you can upload as file in your server Commented May 28, 2016 at 11:11
  • Well i found this method simpler. But if you have an example well explained please give it to me. It's been hours that' I'm blocked here! and I don't understand the problem Commented May 28, 2016 at 11:14
  • if you save image as data is too heavy, which language you are using in server side Commented May 28, 2016 at 11:19
  • I'm using spring-mvc I updated my code abov. You can find my server code Commented May 28, 2016 at 11:23
  • You can follow this documentation spring.io/guides/gs/uploading-files Commented May 28, 2016 at 11:28

0

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.