我正在尝试使用angularJs和PHP上传文件。 确切的错误信息是这样的:
ionic.bundle.js:26799 TypeError: Cannot read property 'state_code' of null
at controllers.js:1718
at services.js:298
at processQueue (ionic.bundle.js:29132)
at ionic.bundle.js:29148
at Scope.$eval (ionic.bundle.js:30400)
at Scope.$digest (ionic.bundle.js:30216)
at Scope.$apply (ionic.bundle.js:30508)
at done (ionic.bundle.js:24829)
at completeRequest (ionic.bundle.js:25027)
at XMLHttpRequest.requestError (ionic.bundle.js:24978)
html表格:
<div class="footer row">
<div class="text-center col col-50">
<div id="FileUpload">
<input type="file" accept="image/*;capture=camera" id="BrowserHidden" ng-model="upload_file"/>
<div class="text-center">
<button class="button custom-button" type="submit">
Charger un fichier
</button>
</div>
</div>
</div>
<div class="text-center col col-50">
<button class="button custom-button" ng-click="SubmitUpload();">
Suivant
</button>
controllers.js在1718看起来像那样:
if (document.getElementById("BrowserHidden") != undefined) {
document.getElementById("BrowserHidden").onchange = function () {
var reader = new FileReader();
reader.onload = function (e) {
// get loaded data and render thumbnail.
document.getElementById("image").src = e.target.result;
};
// read the image file as a data URL.
reader.readAsDataURL(this.files[0]);
$scope.fileUploaded = this.files[0];
var image_name=this.files[0].name;
$scope.image_name=image_name;
localStorage.setItem("piece1", $scope.image_name);
};
}
$scope.SubmitUpload = function () {
if ($scope.fileUploaded == undefined) {
$ionicPopup.alert({
title: "Alert",
template: "Veuillez sélectionner une photo de votre carte d'identité ou passeport"
});
} else {
UploadService.uploadFile($scope.fileUploaded, getLocalItem("id_user", localStorageService), function (data) {
ligne 1718-> console.log(data)
if (data.state_code == 200) {
//$state.go('success_upload');
$state.go('signaletique_upload_2');
} else {
$ionicPopup.alert({
title: "Alert",
template: data.message
});
}
});
}
}
service.js
.factory('UploadService', function ($http, $ionicLoading, Upload) {
return {
uploadFile: function (_photo, _formID, callback) {
$ionicLoading.show();
Upload.upload({
url: UPLOAD_FILE,
headers: {
'Content-Type':'application/x-www-form-urlencoded'
// or 'Content-Type':'application/json'
},
data: {photo: _photo, 'user_id': _formID}
}).then(function (resp) {
ligne 298-> callback(resp.data);
$ionicLoading.hide();
}, function (resp) {
callback(resp.data);
$ionicLoading.hide();
}, function (evt) {
});
}
}
})
function.js
var UPLOAD_FILE = URL_RACINE +'php/ws_upload_file.php';
ws_upload_file.php
include_once 'config.php';
$id_user = $data['user_id'];
$uploads_dir = 'uploads';
$tmp_name = $_FILES["photo"]["tmp_name"];
$name = $_FILES["photo"]["name"];
$ext = pathinfo($name, PATHINFO_EXTENSION);
if(!in_array($ext,['png','jpg','jpeg'])){
echo json_encode(array('status'=>'Error','state_code' => '101','message'=>'fichier invalide'));
die;
}else{
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}