File or image upload by converting into base64 string


#1

So far we used this below code,but we did not got it,its not converted to base64…

So,please anybody help me with this

am using ionic 3.9.2 version
and am also using ionic base64 plugin but its not workedout!!

public takePicture(sourceType) {
// Create options for the Camera Dialog
var options = {
quality: 100,
sourceType: sourceType,
saveToPhotoAlbum: false,
correctOrientation: true
};

// Get the data of an image
this.camera.getPicture(options).then((imagePath) => {
  // Special handling for Android library
  if (this.platfrm.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
    this.filePath.resolveNativePath(imagePath)
      .then(filePath => {
        this.photos.push(imagePath);
        let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
        let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
        this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
     
      });
  } else {
    this.photos.push(imagePath);
    var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
    var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
    this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
  
  }
}, (err) => {
  this.presentToast('Error while selecting image.');
});

}
private createFileName() {
var d = new Date(),
n = d.getTime(),
newFileName = n + “.jpg”;
return newFileName;
}

// Copy the image to a local folder
private copyFileToLocalDir(namePath, currentName, newFileName) {

this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
  this.lastImage = newFileName;
 this.tempurl = namePath +""+ currentName +""+ newFileName;


}, error => {
  this.presentToast('Error while storing file.');
});

}

private presentToast(text) {
let toast = this.toastCtrl.create({
message: text,
duration: 3000,
position: ‘top’
});
toast.present();
}

// Always get the accurate path to your apps folder
public pathForImage(img) {
if (img === null) {
return ‘’;
} else {
return cordova.file.dataDirectory + img;
}
}
/// Upload image into service ////

uploadImage() {

console.log("imabge upload. . . ");



 

 let tempurl = "http://graylogic.net/Nat1Admin/api/NATMasters/MultipleFileUpload";
 var url = encodeURI(tempurl);

var targetPath = this.pathForImage(this.lastImage);

var filename = this.lastImage;

let options: FileUploadOptions = {

fileKey: "recFile",
fileName: filename,
chunkedMode: false,
mimeType: "multipart/form-data",
params : {'ID': 28418}
};


const fileTransfer: FileTransferObject = this.transfer.create();




let trustAllHosts = true;

this.loading = this.loadingCtrl.create({
content: 'Uploading...',
});
this.loading.present();

fileTransfer.upload(targetPath, url, options).then(data => {

this.loading.dismiss();

let toast = this.toastCtrl.create({
message: 'Image successfully Uploaded',
duration: 3000
});
toast.present();


 var result = JSON.stringify(data).slice(135, JSON.stringify(data).indexOf('</string>'));





alert("uploaded Data Image - "+ result);

}, err => {

this.loading.dismiss();

let toast = this.toastCtrl.create({
message: 'Image uploading cancelled.',
duration: 3000
});
toast.present();

});

}