Usecase:
The user selects multiple images from the camera roll, and adds them to their post. They type a description and title, and then choose to post. Their post appears on their user profile, with their selected images attached.
Problem:
I am creating an image blog app with Ionic 4, AngularFire2. My goal is to upload the users images to the firebase storage and download each of the image urls to reference in the Firestore database.
I can upload multiple images to the firebase storage, however I want to download each of these image urls. So far I only can download one image url. I am new to Ionic/AngularFire and I don't know what to loop or where I should loop to get the image urls in an array.
Typescript:
async uploadImageToFirebase(image){
let image_src = this.webview.convertFileSrc(image);
let randomId = Math.random().toString(36).substr(2, 5);
//uploads img to firebase storage
this.firebaseService.uploadImage(image_src, randomId)
.then(photoURL => {
this.image = [ photoURL ]
loading.dismiss();
toast.present();
}, err => {
console.log(err);
})
Service :
encodeImageUri(imageUri, callback) {
var c = document.createElement('canvas');
var ctx = c.getContext("2d");
var img = new Image();
img.onload = function () {
var aux:any = this;
c.width = aux.width;
c.height = aux.height;
ctx.drawImage(img, 0, 0);
var dataURL = c.toDataURL("image/jpeg");
callback(dataURL);
};
img.src = imageUri;
};
uploadImage(imageURI, randomId){
return new Promise<any>((resolve, reject) => {
let storageRef = firebase.storage().ref();
let imageRef =
storageRef.child('image').child(randomId);
this.encodeImageUri(imageURI, function(image64){
imageRef.putString(image64, 'data_url')
.then(snapshot => {
snapshot.ref.getDownloadURL()
.then(res => resolve(res))
}, err => {
reject(err);
})
})
})
}
My firestore database currently looks like this
posts : {
Title : this is a title,
Description: this is a description,
image:
[ 0 ] "https://firebasestorage.googleapis.com/v0/b/imagepost-1962c.appspot.com/o/image%2Fbak95?alt=media&token=58eb6037-4253-4dcc-b35b-8d58ddsdffss"
}
But I want it to look like
posts : {
Title : this is a title,
Description: this is a description,
image:
[ 0 ] "https://firebasestorage.googleapis.com/v0/b/imagepost-1962c.appspot.com/o/image%2Fbak95?alt=media&token=58eb6037-4253-4dcc-b35b-8d58ddsdffss"
[ 1 ] "another image url"
[ 2 ] "another image url"
}