1
我想我的问题已经够清楚了,但在这里有一些细节。我对Angular和PHP非常陌生,所以我可能犯了很多错误或错过了重要的东西:)。
我首先有一个包含文本和文件输入的表单。就像这样:
<div><label for="textInput">Text input</label></div>
<div><input id="textInput" name="textInput" type="text" ng-model="form.textInput"></div><br/>
<div>Put some file please</div>
<div>
<input id="file1" type="file" name="file1" ng-model="form.file1"><br/>
<input id="file2" type="file" name="file2" ng-model="form.file2">
</div>
要与NG-模型发布的文件我用这个指令:
(function() {
fileInput.$inject = [];
function fileInput() {
var fileTypeRegex = /^file$/i;
return {
restrict: 'E',
require: '?ngModel',
link: link
};
function link(scope, element, attrs, ngModel) {
if (ngModel && element[0].tagName === 'INPUT' && fileTypeRegex.test(attrs['type'])) {
element.on('change', function() {
var input = this;
if ('multiple' in attrs) {
var files = Array.prototype.map.call(input.files, function (file) { return file; });
ngModel.$setViewValue(files);
}
else {
ngModel.$setViewValue(input.files[0]);
}
});
}
}
}
angular.module('ng-file-input', []).directive('input', fileInput);
}());
最后,我用$ HTTP发送数据:
$http({
url: window.API_URL + 'postulate.php',
method:'POST',
data:$scope.form
}).then(function successCallback(response){
console.log(response.data);
console.log($scope.form);
});
在PHP文件中,我只获取$ _POST数据并打印出来:
$rest_json = file_get_contents("php://input");
$_POST = json_decode($rest_json, true);
echo json_encode($_POST);
这就是问题所在。我得到与console.log():
Object {textInput: "the content", file1: Array[0], file2: Array[0]}
Object {textInput: "the content", file1: File, file2: File}
做了很多谷歌和测试,但我无法得到它的工作。
询问我是否需要更多详细信息。
我不能使用'$ _POST'?好我的坏我想我看到这个http://php.net/manual/en/features.file-upload.post-method.php所以我想我可以用'$ _POST'来做到这一点。 然后我有一些问题:你说:“要获得php中的文件,你可以检查'$ _FILES'是否被设置或它不是空的。我试图打印'$ _FILES',但我得到了console.log():'[]' –
你必须在PHP中完成。做一个'echo'或'var_dump',然后'die'这样你就会收到响应。 –
我确实在PHP中使用它。不是吗? console.log()的第一行对应于我的PHP响应。或者我误解了你? –