1

I am struggling converting image to byte array using client side script. I have to convert image to byte array, and pass this array to web service , so that the web services can save the image in server. Any one please help me.

$scope.file=function(user){
	//console.log("hhhhh",angular.toJson($scope.user));
	console.log(ContextService.object);
	$scope.users=ContextService.object;
	console.log($scope.user);
	
	//$scope.file=$scope.file;
	console.log($scope.user.file);
	var file=$scope.user.file;
	
	 var bytes = [];   
           
           var fileReader = new FileReader();

           fileReader.onload = function (e) {    
               var naveen=fileReader.result;                
               for (var i = 0; i < naveen.length; ++i)
               {
                   bytes.push(naveen.charCodeAt(i));
                   bytes.push(0);
               }
             console.log("bytes",bytes);
      $scope.call(bytes);
       
           };
           fileReader.onerror = function(err) {
               console.log(err);
           };
           fileReader.readAsBinaryString(file); 
           
           $scope.call=function(bytes)
           {
            $scope.image=bytes;
            
           }
	

            $timeout(function() {
            console.log("vvvvv",$scope.image);   
        	$scope.users.file=$scope.image;
            console.log("dsfdsfds",$scope.users);
            
        	 
        	 var url = "http://127.0.0.1:8080/api/signup";
             $http({
             	method : "POST",
             	url : url,
             	data :angular.toJson($scope.users),
             	headers : {

             		"content-type" : "application/json",
             		"Accept" : "application/json"
             	}

             })

             .success(function(data) {
             	console.log("Detail", angular.toJson(data));
             	$state.go("login");
             });
            
               }, 3000);
          

2
  • var bytes = []; var fileReader = new FileReader(); fileReader.onload = function (e) { var naveen=fileReader.result; for (var i = 0; i < naveen.length; ++i) { bytes.push(naveen.charCodeAt(i)); bytes.push(0); } console.log("bytes",bytes); $scope.call(bytes); }; fileReader.onerror = function(err) { console.log(err); }; fileReader.readAsBinaryString(file); $scope.call=function(bytes) { $scope.image=bytes; } Commented Jun 6, 2016 at 6:54
  • Maybe this will help: stackoverflow.com/a/1323024/3690438 Commented Jun 6, 2016 at 7:42

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.