0

I've been able to sent FormData from angularJS to php, but I don't know how to do the reverse. I'm currently trying to get the $base64 variable into my angularJS, but I'm quite stumped on how to go about doing so. The documentation in the official angularJS doesn't help me much either

https://docs.angularjs.org/api/ng/service/$http

JS

$scope.MakeGray_Button = function(){
    if ($scope.imageUrl) {
        var MakeGray_Form = new FormData();
        MakeGray_Form.append("FileName", $scope.imageUrl);
        $http({
        method : "POST",
        url    : "../opencv/MakeGray/MakeGray.php",
        data   : MakeGray_Form,
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
        }).
        success(function(){                     
           // some magic code here that grabs the $base64 variable from php
        })
        .error(function(){});
    }
    else{
        alert("Please upload an image");
    }
}

PHP

<?php

$imgname = $_POST["FileName"];
$inputDir = "../../uploads/" . $imgname;
$outputDir = "../../processed/" . $imgname;

$MakeGray = "./makegray " . $inputDir . " " . $outputDir;
$runExec  = exec($MakeGray, $out);

$type = pathinfo($outputDir, PATHINFO_EXTENSION);

$data = file_get_contents($outputDir);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

echo "$base64";

?>
Zypps987
  • 404
  • 6
  • 21

2 Answers2

1

You can add a parameter in success callback to fetch the response from server if available.

var base64 = ''; 
$http({
    method : "POST",
    url    : "../opencv/MakeGray/MakeGray.php",
    data   : MakeGray_Form,
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined}
    }).
    success(function(response){                     
          base64 = response; //response will contain whatever server echos to it's standard output
    })
    .error(function(){});
McLosys Creative
  • 759
  • 4
  • 9
  • 19
1

you can get the response from server by using

.then(function (data) {}

and in "data" variable you can find the value you're looking for

Nick Greg
  • 221
  • 4
  • 7