首页 > 解决方案 > 如何从 Firebase 存储中检索文件(而不仅仅是其下载 URL)

问题描述

也许这是一个愚蠢的问题......但目前我只是不知道如何从firebase存储中获取文件(而不仅仅是它的downloadURL)。

使用 AngularFireStorage (并遵循文档:https://github.com/angular/angularfire2/blob/master/docs/storage/storage.md)我只能获得我想要的文件的下载URL:

private getBusinessCardFromFirebaseStorage() {

    const ref = this.storage.ref(`${this.idOfUser}/businessCard.card`);
    const url = ref.getDownloadURL();
    //this.file = ???;

}

但我需要该文件(为了将其发送到另一台服务器......我需要文件本身......我不能只发送 url)。我怎样才能得到文件?


更新:

我现在尝试使用 firebase API(而不是 AngularFire API)。它仍然不起作用。

具体来说,我尝试了以下方法:

 storageRef.child(`${this.idOfUser}/businessCard.card`).getDownloadURL().then(function(url) {

  var xhr = new XMLHttpRequest();
  xhr.responseType = 'blob';
  xhr.onload = function(event) {  
     var blob = xhr.response;
     //the following line does not work ... the external variable is not accessible here:                   
     this.externalVariable = blob; 
  };
  xhr.open('GET', url);
  xhr.send();
}).catch(function(error) {
  // Handle any errors
});

我还尝试了以下方法:

    var storage = firebase.storage();
    var storageRef = storage.ref();
    var fileRef = storageRef.child(`${this.idOfUser}/businessCard.card`);


    fileRef.getDownloadURL()
        .then(url => {
            //the following line gets never printed:
            console.log('Download URL: ' + url);
            return fetch(url)
        })
        .then(response => response.blob())
        .then(blob => {
            console.log('File downloaded as blob');
            this.businessNetworkCardFile = new File([blob], 'businessNetworkCard.card', {type: 'application/octet-stream', lastModified: Date.now()});
        })
        .catch(error => {
            console.error('Something went wrong while downloading the business network card:', error);
        })

这也不起作用。......这太荒谬了!他们怎么能让这件事变得如此复杂和如此糟糕的记录?!

标签: angularfirebasefirebase-storageangularfire

解决方案


推荐阅读