2016-01-07 52 views
4

我目前的情況下上傳的文件是:我做這樣的後續嵌套的重複:AngularJS:通過角JS

$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它只能打印空。幫我正確地接收到值..

回答

2

在你的PHP文件中刪除譯碼線是:

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

你不需要進行解碼,因爲你沒有數據接收到JSON編碼數組...

+1

感謝兄弟。你已經指出了正確的問題.. –

+1

謝謝。不要忘記將此答案標記爲已接受。 –