return the download URL of a file uploaded to firebase

后端 未结 3 1827
攒了一身酷
攒了一身酷 2021-01-23 04:41

Is there a simple way to get the download URL of a file uploaded to Firebase?

(I\'ve tried playing around with the snapshot returned by my upload function and couldn\'

3条回答
  •  臣服心动
    2021-01-23 05:16

    The documentation referenced by Yamil - Firebase javascript SDK file upload - recommends using the snapshot's ref property to invoke the getDownloadURL method, which returns a promise containing the download link:

    Using your code as a starting point:

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

    I know it seems like unnecessary effort and that you should be able to get the link via a property of the snapshot, but this is what the Firebase team recommends - they probably have a good reason for doing it this way.

提交回复
热议问题