AngularJS:通过angular JS上传文件

时间:2016-01-07 13:26:04

标签: php angularjs

我目前的情况是:我正在做嵌套重复,如下所示:

$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));
    });
}   

但在我的upload_file.php我无法收到以下值:

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

console.log他们工作正常。但在服务器端它没有收到。这是我的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);

但在var_dump上它只打印null。帮助我正确接收值..

1 个答案:

答案 0 :(得分:2)

在你的php文件中删除以下的解码行:

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

您不需要解码因为您没有将数据接收到JSON编码的数组......