Could not upload image any one help me please!1

Onload
ionViewDidLoad() {

const options: CameraOptions = {
  quality: 70,
  destinationType: this.camera.DestinationType.DATA_URL,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE
}

this.camera.getPicture(options).then((imageData) => {
 // imageData is either a base64 encoded string or a file URI
 // If it's base64 (DATA_URL):
 this.myphoto = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
 // Handle error
});
//console.log('ionViewDidLoad CameraPage');

}

Upload Image
uploadImage(){
//Show loading
let loader = this.loadingCtrl.create({
content: “Uploading…”
});
loader.present();
//create file transfer object
const fileTransfer: FileTransferObject = this.transfer.create();

 //random int
 var random = Math.floor(Math.random() * 100);

 //option transfer
 let options: FileUploadOptions = {
   fileKey: 'photo',
   fileName: "myImage_" + random + ".jpg",
   chunkedMode: false,
   httpMethod: 'post',
  mimeType: "image/jpeg",
   headers: {}
 }

 //file transfer action
 fileTransfer.upload(this.myphoto, 'http://localhost:8080/orangehrm/uploadimage/uploadimage.php', options)
   .then((data) => {
     alert("Success");
     loader.dismiss();
   }, (err) => {
     console.log(err);
     alert("Error");
     loader.dismiss();
   });

}

php File

<?php header('Access-Control-Allow-Origin: *'); $target_dir = "/images/"; $target_file = $target_dir . basename($_FILES["photo"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); $check = getimagesize($_FILES["photo"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; if (move_uploaded_file($_FILES["photo"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["photo"]["name"]). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } else { echo "File is not an image."; $uploadOk = 0; } ?>

Couple of things…

The end point you are uploading to is localhost:8080 - that is the same as the wkwebview plugin which may lead to conflict.

Secondly for testing… Are you able to upload a file to your php using curl?

8080 is my port where i install xampp

For the second time… Are you able to upload a file to your php server using curl?

1 Like