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