I can't get image downloadUrl from Firebase St

2019-03-06 14:56发布

问题:

I'm trying to get downloadUrl for an image from firebase, All properties like 'timeCreated', 'fullPath', 'contentType' are working well & pushed correctly! but I don't know why 'downloadUrl' doesn't work!!

captureAndUpload() {
    this.dataProvider.captureImage().then(data => {
     this.dataProvider.uploadImage(data).then(res => {
        this.dataProvider.storeImageInformation(res.downloadURL);
      });

    });
  }

Data provider:

storeImageInformation(downloadURL)
 {
this.db.list(`/profiles/${this.afAuth.auth.currentUser.uid}`).
push(downloadURL); 
      }

Any thoughts?!

回答1:

The download URL is now no longer accessible in the immediate results of the upload. This change was made a few months ago to the Firebase client SDKs.

Instead, you will have to call getDownloadURL (or whatever the Angular binding is for that JavaScript function) to get the URL as a second request after the upload is complete.



回答2:

THX Doug, you're right.

The correct code is like this:

captureAndUpload() {
this.data.captureImage().then(data => {
  let upload = this.data.uploadImage(data);

  upload.then().then(res => {
    this.data.storeImageInformation(res);
  }); 
}

Data Provider:

uploadImage(image) {

let storageRef: AngularFireStorageReference;

let newName = `${new Date().getTime()}-${this.afAuth.auth.currentUser.uid}.png`;

storageRef = this.afStorage.ref(`/images/${this.afAuth.auth.currentUser.uid}/${newName}`);

return storageRef.putString(image, 'base64', { contentType: 'image/png'})
          .snapshotChanges().toPromise().then(_ =>
             {
              return storageRef.getDownloadURL().toPromise().then(res => {
                console.log('URL: ', res);
                return res;
              });
            }
          ) 
        }

 storeImageInformation(downloadURL) {
  return this.db.object(`/images/${this.afAuth.auth.currentUser.uid}`).update({img: downloadURL}); }