Ниже приведен пример кода. Используйте плагин FileTransfer.
https://ionicframework.com/docs/native/file-transfer
import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer/ngx';
import { File } from '@ionic-native/file/ngx';
constructor{
private camera: Camera,
private transfer: FileTransfer,
private file: File,
) {
}
takePhoto(){
const options: CameraOptions = {
quality: 50,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
sourceType: this.camera.PictureSourceType.CAMERA,
correctOrientation: true
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64 (DATA_URL):
this.uploadPhoto(imageData); //call your function to upload image
}, (err) => {
// Handle error
});
}
uploadPhoto(path){
const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
fileKey: 'file',
fileName: '.png',
chunkedMode: false,
//mimeType: "image/jpeg",
}
fileTransfer.upload(path, 'yourUrl.com/imageupload', options)
.then((data) => {
console.log(JSON.parse(data.response));
let res = JSON.parse(data.response);
if (res.status == 1) {
tconsole.log('Image Uploaded successfull');
}
}, (err) => {
console.log(err);
});
}