Hi everyone I’m little stuck at a place. I’m trying to upload a image(not base64) to a rest api but can’t upload. Can anyone please tell me what wrong am I doing???
import { Component } from '@angular/core';
import { NavController, LoadingController, ActionSheetController, ToastController, Platform, Loading } from 'ionic-angular';
import { Camera, CameraOptions } from '@ionic-native/camera';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';
@Component({
selector: 'page-sec-four',
templateUrl: 'sec-four.html'
})
export class SecFourPage {
loading: Loading;
myphoto:any;
imageURI:any;
imageFileName:any;
constructor(public navCtrl: NavController, private camera: Camera, private transfer: FileTransfer, private file: File, public actionSheetCtrl: ActionSheetController, public toastCtrl: ToastController, public platform: Platform, public loadingCtrl: LoadingController) {
}
presentActionSheet() {
let actionSheet = this.actionSheetCtrl.create({
title: 'Select Image Source',
buttons: [
{
text: 'Load from Library',
handler: () => {
const options: CameraOptions = {
quality: 70,
destinationType: this.camera.DestinationType. FILE_URI,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
saveToPhotoAlbum:false
}
this.camera.getPicture(options).then((imageData) => {
this.imageURI = imageData;
}, (err) => {
// Handle error
});
}
},
{
text: 'Use Camera',
handler: () => {
const options: CameraOptions = {
quality: 70,
destinationType: this.camera.DestinationType. FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
this.camera.getPicture(options).then((imageData) => {
this.imageURI = imageData;
console.log(this.camera.DestinationType. FILE_URI);
}, (err) => {
// Handle error
});
}
},
{
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('Cancel clicked');
}
}
]
});
actionSheet.present();
}
private presentToast(text) {
let toast = this.toastCtrl.create({
message: text,
duration: 3000,
position: 'top'
});
toast.present();
}
uploadImage(){
//Show loading
this.loading = this.loadingCtrl.create({
content: "Uploading..."
});
this.loading.present();
//create file transfer object
const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
fileKey: 'photo',
fileName: 'pulsepic',
chunkedMode: false,
httpMethod: 'post',
mimeType: "image/jpeg",
params : {uploaddoc: 'imageURI'}
}
//file transfer action
fileTransfer.upload(this.imageURI, 'https://myrestAPI.php', options)
.then((data) => {
this.loading.dismissAll()
this.presentToast('Image succesful uploaded.');
}, (err) => {
this.loading.dismissAll()
this.presentToast('Error while uploading file.');
}).catch(err=> console.log(err));
}
}