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}}">