I can't get image downloadUrl from Firebase Storage (Angular/Ionic)

流过昼夜 提交于 2019-12-31 05:13:06

问题


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}); }


来源:https://stackoverflow.com/questions/53231025/i-cant-get-image-downloadurl-from-firebase-storage-angular-ionic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!