angular_kid
angular_kid

Reputation: 43

AngularJS: Uploading file via angular JS

My current scenario is: I've doing nesting repetition like follow:

$scope.uploadPic = function(file) 
{

    alert($scope.taskdetails.id);       //task_id e.g 21
    alert($rootScope.job_id);   //job_id e.g 12
    file.upload = Upload.upload(
    {
      url: 'http://localhost/mobile-data/upload_file.php',
      data: {
                file: file,
                task_id: $scope.taskdetails.id,
                job_id: $rootScope.job_id
            },

    });
    file.upload.then(function (response) {
      $timeout(function () {
        file.result = response.data;
      });
    }, function (response) {
      if (response.status > 0)
        $scope.errorMsg = response.status + ': ' + response.data;
    }, function (evt) {
      // Math.min is to fix IE which reports 200% sometimes
      file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
    });
}   

but on my upload_file.php i can't receive the values for:

task_id: $scope.taskdetails.id,
job_id: $rootScope.job_id

in console.log they are working fine. but on server side it is not receiving. here is code of my upload_file.php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('content-type: application/json; charset=utf-8');
$_POST = json_decode(file_get_contents('php://input'), true);

$task_id = $_POST["task_id"];
$file = $_FILES["file"];
$job_id = $_POST["job_id"];
var_dump($task_id);
var_dump($job_id);

but on var_dump it only print null. Help me to receive the values correctly..

Upvotes: 4

Views: 59

Answers (1)

Tech Kid
Tech Kid

Reputation: 577

In your php file remove the decode line that is:

$_POST = json_decode(file_get_contents('php://input'), true);

You dont need to decode because you are not receiving data into JSON encoded array...

Upvotes: 2

Related Questions