我试图找到一种方法,使用FormData上传一些参数和文件,但不知道在后端查找它的位置。
HTML:
<input type="file" file-model="myFile"/>
<button class="btn" ng-click="uploadFile()">upload</button>
指令:
.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}])
控制器:
var file = $scope.aFile;
var foo = 'bar';
var fd = new FormData();
fd.append('file', file);
fd.append('foo', foo);
$http.post(api, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).then(function(res){
console.log(res.data);
});
后端
$_POST = json_decode(file_get_contents('php://input'),true);
$result = saveFile('./img/', 'a_test_file'); /* SUCCEEDS! */
$res = array($_POST, $_FILES, $result); /* $_POST empty, $_FILES ok, $result success */
/* where is foo?? */
不确定我缺少什么或在哪里寻找附加参数?