1
votes

The code below successfully uploads images to Firebase storage, but I want to retrieve the downloadURL of that exact image and store it in "this.pichaMtumiaji" and later save it in Firebase database. I'm using ionic 3!

uploadPhoto(): void {
    this.myPhotosRef.child(this.generateUUID()).child('myPhoto.png')
      .putString(this.myPhoto, 'base64', { contentType: 'image/png' })
      .then((savedPicture) => {
        this.pichaMtumiaji = savedPicture.downloadURL().absoluteString;
      });
  }

generateUUID(): any {
  var d = new Date().getTime();
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx'.replace(/[xy]/g, function (c) {
    var r = (d + Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
}
1

1 Answers

1
votes

Based on the sample in the Firebase documentation on uploading a file:

var ref = this.myPhotosRef.child(this.generateUUID()).child('myPhoto.png');
ref
  .putString(this.myPhoto, 'base64', { contentType: 'image/png' })
  .then((savedPicture) => {

    // Upload completed successfully, now we can get the download URL
    ref.getDownloadURL().then(function(downloadURL) {
      console.log('File available at', downloadURL);
      this.pichaMtumiaji = savedPicture.downloadURL().absoluteString;
    });

  });