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

?>
3
  • maybe this helps you: stackoverflow.com/questions/19383725/… Commented Dec 27, 2015 at 13:59
  • I don't see how this is related? My question was related to the php backend. Commented Dec 27, 2015 at 14:06
  • @TheVillageIdiot i could really use your help with this one! Commented Dec 27, 2015 at 17:29

2 Answers 2

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(){});
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.