Hi All,
I’ve been trying to upload multiple images to my Firebase from an Array which holds the images which a user selects from their device.
Currently, when a user selects 2 or more images. only one image uploads to the database. I am relatively new to Ionic development - all help is appreciated!
Thanks in advance - please find current code below
getImage() {
const optionsGallery: CameraOptions = {
quality: 100,
targetWidth: 1080,
targetHeight: 1080,
mediaType: this.camera.MediaType.PICTURE,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
};
this.camera.getPicture(optionsGallery)
.then((imageData) => {
// imageData is either a base64 encoded string or a file URI // If it's base64:
this.captureDataUrl = 'data:image/jpeg;base64,' + imageData;
this.productImages.push(this.captureDataUrl);
}, (err) => {
// Handle error
console.log(err);
});
}
upload() {
this.productImages.array.forEach(element => {
let storageRef = firebase.storage().ref();
// Create a timestamp as filename
const filename = Math.floor(Date.now() / 1000);
// Create a reference to 'images/todays-date.jpg'
const imageRef = storageRef.child( firebase.auth().currentUser.uid + `/${filename}.jpg`);
let i = 0;
for (i; i < this.productImages.length; i++) {
console.log('After the for loop' + this.productImages.length);
imageRef.putString(this.productImages[i], firebase.storage.StringFormat.DATA_URL)
.then((snapshot) => {
// Do something here when the data is succesfully uploaded!
this.showSuccesfulUploadAlert();
}).catch((err) => {
console.log(err.message);
});
// }
});
}
async showSuccesfulUploadAlert() {
const toast = await this.toastController.create({
message: 'Image uploaded successfully',
duration: 5000
});
toast.present();
// clear the previous photo data in the variable
// this.captureDataUrl = "";
console.log('This is the last line: ' + this.captureDataUrl.length);
}