Php AngularJS:通过AngularJS上传文件
我目前的情况是:我做了如下嵌套重复:Php AngularJS:通过AngularJS上传文件,php,angularjs,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_f
$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\u 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\u dump
上,它只打印空值。帮助我正确接收值。在php文件中删除解码行,即:
$_POST = json_decode(file_get_contents('php://input'), true);
您不需要解码,因为您没有将数据接收到JSON编码的数组中