错误上传文件,php和angularjs connexion中止

时间:2018-02-01 12:11:28

标签: javascript php angularjs ionic-framework

我正在尝试使用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");
}
  • evrything曾经工作得很好,但现在我得到了连接错误 中止,当我尝试上传文件。所以我找出一个解决方案 在调用ws php上传文件之前添加主机名 exp(http://195.25.55.21:81/php/ws_upload.php)但这只适用于 我的本地机器其他人甚至连他们都可以联系 访问域名,他们尝试时只获得错误权限 上传文件。
  • 当我尝试调用函数时让它更加混乱 那个('/ php / ws_upload.php')只接受小小的文件, 即使在php.ini默认情况下我得到2M,请注意这个方法我得到 连接中止无法加载ressource /php/ws_upload.php。
  • 我爱下了这个项目,我在我的机器上尝试了evrything工作正常 ,意味着它不是我的代码中的错误
  • 我只是不确定什么可以阻止上传,让人们想知道。谢谢

0 个答案:

没有答案