I have an ionic 3 app; I am working on upload profile picture functionality. In this, I want to select the image from either gallery or capture image using the camera. After that, I will have two images/image_paths. I want to upload these two images along with user_id, access_token
Select Image From Gallery let option = { title: 'Select Picture', message: 'Select Least 1 Picture', maximumImagesCount: 1, outType: 0 };
this.imagePicker.getPictures(option).then(results => {
for (var i = 0; i < results.length; i++) {
// alert('Image URI: ' + results[i]);
this.imageSelected = "data:image/jpeg;base64," +results[i];
// this.imageSelected = results[i];
let option = {
quality: 100,
targetHeight: 400,
targetWidth: 400,
};
this.crop.crop(this.imageSelected, option).then((data) => {
this.imageCropped = "data:image/jpeg;base64," +data;
// alert(this.imageCropped);
this.saveProfileImage(this.imageSelected, this.imageCropped);
}, err => {
this.imageCropped = '';
alert(err);
});
}
}, err => {
this.imageSelected = '';
alert('Error' + err);
})
Select an image from the camera let options: CameraOptions = { quality: 100, destinationType: this.camera.DestinationType.DATA_URL, encodingType: this.camera.EncodingType.JPEG, mediaType: this.camera.MediaType.PICTURE }
this.camera.getPicture(options).then((imageData) => {
// alert(imageData);
this.imageSelected = "data:image/jpeg;base64," +imageData;
let option = {
quality: 100,
targetHeight: 400,
targetWidth: 400,
};
this.crop.crop(this.imageSelected, option).then((data) => {
this.imageCropped = "data:image/jpeg;base64," +data;
this.saveProfileImage(this.imageSelected, this.imageCropped);
}, err => {
this.imageCropped = '';
alert(err);
});
}, (err) => {
this.imageSelected = '';
alert('Error' + err);
});
Please see the above code and if it is right, suggest me how to write upload function with either form data or any another method
Even I was facing the same problem some time back, and did not find proper solution on the web. Below is the solution, which I found after some research and works perfectly fine, not just for images, but also for other files. Since Question asked is about the images, I will explain the answer w.r.t to images. (Except choosing the files, the procedure remains same for other files).
code for choosing the image from camera:
const options: CameraOptions = {
quality: 100,
correctOrientation: true,
cameraDirection: 1,
}
this.camera.getPicture(options).then((imageData) => {
console.log("IMAGE DATA IS", imageData);
}).catch(e => {
console.log("Error while picking from camera", e)
})
code for choosing the image from gallery:
var options = {
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
destinationType: this.camera.DestinationType.FILE_URI,
};
this.camera.getPicture(options).then((imageData) => {
console.log("IMAGE DATA IS", imageData);
}).catch(e => {
console.log("Error while picking from gallery", e)
});
Below is the code snippet to convert the chosen image to base64,I have written it for camera, it remains same for gallery. After converting the image to base64 push it to an array. Now you are able to choose multiple images and store it's value in array.
var options = {
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
destinationType: this.camera.DestinationType.FILE_URI,
};
this.camera.getPicture(options).then((imageData) => {
console.log("IMAGE DATA IS", imageData);
let filePath: string = imageData
this.base64.encodeFile(filePath).then((base64File: string) => {
console.log(base64File);
this.base64File.push(base64File);
}, (err) => {
console.log(err);
});
}).catch(e => {
console.log("Error while picking from gallery", e)
});
I have also created a git hub repository at: https://github.com/coolvasanth/upload-multiple-image-files-in-Ionic-3-4/blob/master/README.md