jornve's avatar

Laravel - Cordova File Transfer error 1 Laravel

I'm working on an Ionic angular 1 app where I want to upload a picture to my laravel api server. However I'm getting error 1 (File not found?) and an laravel php error as error body back. I've followed multiple examples for uploading files this way, I keep getting this errors..

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);
      }
    };

So here I'm storing my pictures in an array because I want to display them in the app and make a queue for the upload process.

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 function :

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

Errors I get : Cordova Error Code 1

Laravel error : ErrorException in Factory.php line 91:\n Argument 2 passed to Illuminate\Validation\Factory::make() must be of the type array, null given, called in D:\PROGRAMS\wamp64\www\ac-laravel\bootstrap\cache\compiled.php

Anyone an idea? Maybe I can't just give the image URI to the upload process?

0 likes
2 replies
Sytham003's avatar

Any update about this? I'm having the same problem

Sytham003's avatar

Found the error, reason was the rules function in my Request model wasn't giving an array back

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

Please or to participate in this conversation.