2017-10-21 66 views
1

我有這樣的代碼:離子3 +火力地堡貯存獲取簡介的圖片

/////////////////////////////////////////////CROP + UPLOAD FOR FIREBASE STORAGE////////////////////////// 

    // Return a promise to catch errors while loading image 
    getMedia(): Promise<any> { 
    // Get Image from ionic-native's built in camera plugin 
    return Camera.getPicture(this.options) 
     .then((fileUri) => { 
     // Crop Image, on android this returns something like, '/storage/emulated/0/Android/...' 
     // Only giving an android example as ionic-native camera has built in cropping ability 
     if (this.platform.is('ios')) { 
      return fileUri 
     } else if (this.platform.is('android')) { 
      // Modify fileUri format, may not always be necessary 
      fileUri = 'file://' + fileUri; 
      /* Using cordova-plugin-crop starts here */ 
     return Crop.crop(fileUri, { quality: 100 }); 
     } 
     }).then((path) => { 
     // path looks like 'file:///storage/emulated/0/Android/data/com.foo.bar/cache/1477008080626-cropped.jpg?1477008106566' 
     // console.log('Cropped Image Path!: ' + path); 
     path; // return the path of file 
     window.resolveLocalFileSystemURL(path, FE=>{ 
      FE.file(file=>{ 
      const FR=new FileReader() 
      FR.onloadend = ((res: any)=>{ 
       let AF=res.target.result 
       let blob=new Blob([new Uint8Array(AF)], {type: 'image/jpg'});  
       this.upload(blob) 
      }); 
      FR.readAsArrayBuffer(file); 
      }) 
      }) 
     }) 
     } 

     upload(blob:Blob){ 
     const currentUserId = this.fire.auth.currentUser.uid; // get user UID in firebase 
     this.Fbref.child(`Profile/${currentUserId}/img`).put(blob); //path in firebase storage 


     ////GET Image URL 
     this.Fbref.child(`Profile/${currentUserId}/img`).getDownloadURL().then(function(url){ 
     console.log("the URL Image is: " + url); 
     url; 
     let photoURL = this.url 

});} 

它可以正常使用,不會對圖像剪切,然後上傳....但我不能保存的URL Firestore數據庫中用戶配置文件DOC內的圖像。

需要把爲photoURL:(URL圖片)

有誰知道怎麼辦呢?

回答

1

[解決] 添加下面的代碼工作完全

savephotoURL(){ 
    const currentUserId = this.fire.auth.currentUser.uid; 
    this.Fbref.child(`Profile/${currentUserId}/img`).getDownloadURL().then(function(url){ 
    console.log("the URL Image is: " + url); 
    let imageURL = url 
    return imageURL 
}).then((imageURL) => { 
    this.database.doc(`ProfileUser/${currentUserId}/`).update({photoURL:imageURL}) })// save url in Firestore database realtime 
} 
+0

你好@Juliano JC你對此有回購?上傳和顯示圖像離子3與firebase我遇到一些問題。我會感謝您的反饋。謝謝。 –

+0

你好! ...我現在創建了一個倉庫,它位於下面的鏈接中,我希望它可以幫助你...我還花時間讓代碼工作..如果有任何疑問,請將它發送到github [https:///github.com/Julianojc/Firebase-Storage-Upload] –

+0

讓我看看,非常感謝。 –