返回上传到firebase的文件的下载URL

Dav*_* J. 1 javascript snapshot firebase firebase-storage

有没有一种简单的方法可以将文件的下载URL上传到Firebase?

(我试过玩上传功能返回的快照,找不到任何东西......)

     fileref.put(file).then(function(snapshot){
     self.addEntry(snapshot);   
     ///  return snapshot.url???
     });
Run Code Online (Sandbox Code Playgroud)

Pez*_*eza 17

Yamil引用的文档 - Firebase javascript SDK文件上传 - 建议使用snapshot的ref属性来调用该getDownloadURL方法,该方法返回包含下载链接的promise:

使用您的代码作为起点:

fileref.put(file)
   .then(snapshot => {
       return snapshot.ref.getDownloadURL();   // Will return a promise with the download link
   })

   .then(downloadURL => {
      console.log(`Successfully uploaded file and got download link - ${downloadURL}`);
      return downloadURL;
   })

   .catch(error => {
      // Use to signal error if something goes wrong.
      console.log(`Failed to upload file and get link - ${error}`);
   });
Run Code Online (Sandbox Code Playgroud)

我知道这似乎是不必要的努力,你应该能够通过快照的属性获得链接,但这是Firebase团队推荐的 - 他们可能有充分的理由这样做.