如何从Firebase存储中检索文件(而不是仅下载URL)

时间:2018-08-15 15:17:44

标签: angular firebase firebase-storage angularfire

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

使用AngularFireStorage(并遵循文档:https://github.com/angular/angularfire2/blob/master/docs/storage/storage.md),我可以获取所需文件的downloadURL:

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

这也不起作用。 ...太荒谬了!他们如何才能使这件事变得如此复杂且记录不清?

0 个答案:

没有答案