Can't upload a photo fileTransferError {code = 1}

Hi
I’m trying to upload a photo and when I try to upload the photo I get this error message :

        FileTransferError {
            code = 1;
            source = "http://domain.com/api/upload_photo.php";
        target = "file:///var/mobile/Containers/Data/Application/85CF7FAF-4E5B-4ABC-910D-F9F6E1A60C66/tmp/cdv_photo_002.jpg";
    }

here is my server code (php) :
note : of course I didn’t put the url : domain.com I have a real domain in there but I didn’t put it for security reasons

<?php
header('Access-Control-Allow-Origin: *');
$target_path = "uploads/"; 
$target_path = $target_path . basename( $_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
$response['code'] = '200';
$response['message'] = 'the file was uploaded successfuly';
echo json_encode($response);
} else {
		$response['code'] = '201';
		$response['error'] = 'there are missing arguments' ;
		echo json_encode($response);	
}
?> 

and here is my ionic code :

  takePhoto(){
console.log('test1');
var options: CameraOptions = {
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE
}

console.log('test2');
this.camera.getPicture(options).then((imageData) => {
  console.log('test3');

  let base64Image = 'data:image/jpeg;base64,' + imageData;
  console.log(imageData);
  this.residency_image = imageData;
  console.log(this.residency_image);
  console.log('test4');
}, (err) => {
  console.log('test5');
  // Handle error
});
console.log('test6');
  }

  savePhoto(){
console.log(this.residency_image);

// Destination URL
var url = "http://trucksadress.com/api/upload_photo.php";

// File for Upload
var targetPath = this.pathForImage(this.residency_image);
console.log(targetPath);
console.log(this.residency_image);
// File name only
var filename = this.residency_image;

var options = {
  fileKey: "file",
  fileName: filename,
  chunkedMode: false,
  mimeType: "multipart/form-data",
  params : {'id': "1234"}
};
const fileTransfer: TransferObject = this.transfer.create();
fileTransfer.upload(url, targetPath, options).then(function(result) {
console.log(result);
});



  }

  pathForImage(image) {
    if (image === null) {
      return '';
    } else {
      return File.dataDirectory + image;
    }
  }

What is [quote=“keloa, post:1, topic:86522”]
this.transfer
[/quote]
? Would help to classify the error.

here is my constructor to be more clear :

constructor(public transfer: Transfer, private file: File,public camera : Camera ,public navCtrl: NavController, public navParams: NavParams, public http : HTTP , form : FormBuilder){}

This https://ionicframework.com/docs/native/transfer/?

yes that is exactly what I used

Should have posted this at the beginning…

Well, then this is the error:

GitHub - apache/cordova-plugin-file-transfer: Apache Cordova File Transfer Plugin
GitHub - apache/cordova-plugin-file-transfer: Apache Cordova File Transfer Plugin

1 = FileTransferError.FILE_NOT_FOUND_ERR

Shouldn’t the first param be the file to be uploaded and the second the URL to upload to?
https://github.com/apache/cordova-plugin-file-transfer#upload

From what I understand of your code you have it the other way around.

thanks this worked !

I feel so stupid right now XD

1 Like