AngularJS:通过Angular JS上传文件

4

我的当前情况是:我正在进行类似以下的嵌套重复:

$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个回答

2

在您的php文件中删除解码行,即:

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

您无需解码,因为您没有收到JSON编码数组的数据...



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接