Cordova文件传输错误1 Laravel

时间:2016-09-26 08:35:59

标签: angularjs cordova laravel ionic-framework file-transfer

我正在开发一个Ionic angular 1应用程序,我想将图片上传到我的laravel api服务器。但是我收到错误1(找不到文件?)和laravel php错误作为错误正文。我已经按照这种方式跟踪了多个上传文件的例子,我不断收到这个错误..

Angular Code:

     $scope.takePicture = function () {
              var options = {correctOrientation: true,
                saveToPhotoAlbum: true,
                encodingType: 0,
                sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
                mediaType: Camera.MediaType.PICTURE,
                destinationType: Camera.DestinationType.FILE_URI};
              getPicture(options);
            };

  var getPicture = function(options) {
          navigator.camera.getPicture(onCapturePhoto, onFail, options);
        };
        function onFail(message) {
          alert('Failed because: ' + message);
            }

 function onCapturePhoto(fileURI) {
              $scope.pictures.push(fileURI);
            }

  $scope.uploadPhoto = function () {
          var repairJobId = 1;
          for(var i=0; i<$scope.pictures.length;i++){
                repairJobService.uploadPhoto($scope.pictures[i],repairJobId);
          }
        };

所以这里我将我的图片存储在一个数组中,因为我想在应用程序中显示它们并为上传过程创建一个队列。

Angular Service

 function uploadPhoto(file,repairJobId) {
              var win = function (r) {
                console.log("Code = " + r.responseCode);
                console.log("Response = " + r.response);
                console.log("Sent = " + r.bytesSent);
              };

              var fail = function (error) {
                alert("An error has occurred: Code = " + error.code);
                console.log("upload error source " + error.source);
                console.log("upload error target " + error.target);
                console.log("response code " + error.responseCode);
                console.log("error:  " + JSON.stringify(error));
              };

              // Destination URL
              var url = 'http://192.168.137.1/ac-laravel/public/api/photo/add';

              // File name only
              var filename = file.split("/").pop();
              console.log("filename:"+ filename);

              var headers={'X-Requested-With':'XMLHttpRequest'};

              var options = new FileUploadOptions({
                fileKey: "photo",
                fileName: filename,
                chunkedMode: false,
                headers: headers,
                params : {'repair_job_id':repairJobId} // directory represents remote directory,  fileName represents final remote file name
              });


              var ft = new FileTransfer();
              console.log(file, encodeURI(url));
               ft.upload(file, encodeURI(url),win, fail, options, true);

        }

Laravel功能:

 public function storeRepairJobPhotos2($request)
   {
       $destinationPath = 'uploads/';
       $newImageName='MyImage.jpg';
       $request::file('photo')->move($destinationPath,$newImageName);
   }

我得到的错误:Cordova错误代码1

Laravel错误:Factory.php第91行中的ErrorException:\ n传递给Illuminate \ Validation \ Factory :: make()的参数2必须是数组类型,给定null,在D:\ PROGRAMS \ wamp64 \ www中调用\ AC-laravel \自举\缓存\ compiled.php

有人有想法吗?也许我不能只将图片URI提供给上传过程?

1 个答案:

答案 0 :(得分:0)

发现错误,原因是我的Request模型中的规则函数没有返回数组

 public function rules()
    {
        return [
            'photo'          => 'required| mimes: jpg,jpeg,png'
        ];
    }