angular - 如何从 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);
})
这也不起作用。......这太荒谬了!他们怎么能让这件事变得如此复杂和如此糟糕的记录?!
解决方案
推荐阅读
- angular - MatAutocompleteTrigger optionSelections Observable 停止触发
- reactjs - Infowindow 每次都用数千个标记重新渲染整个谷歌地图
- docker - 我正在尝试在 ubuntu 容器上安装 gitlab 综合包
- javascript - 在 JavaScript append 中循环动态数据
- angular - 为什么在使用 ngFor 时将数组视为对象
- scala - Scala在Windows上执行命令
- java - 如何在 TextView 中显示 UUID?
- solr - 你知道如何设置 solr not present _"str" 吗?
- r - 如何根据 R 中的索引将列添加到数据框中?(见示例)
- tcp - 如何计算 TCP 的吞吐量?