【发布时间】:2020-04-19 07:29:42
【问题描述】:
我有一个应用程序正在使用 Firebase 存储来发送一些图像数据,所以我使用这种方法:
startUpload() {
if (typeof this.fileList !== 'undefined' && this.fileList.length > 0) {
const observableList = [];
for (let i = 0; i < this.fileList.length; i++) {
// The storage path
const path = this.userModel.companyCode + `/${new Date().getTime()}_${this.fileList[i].name}`;
// Totally optional metadata
const customMetadata = {
app: 'My AngularFire-powered PWA!'
};
const fileRef = this.storage.ref(path);
// The main task
this.task = this.storage.upload(path, this.fileList[i], {
customMetadata
});
// Progress monitoring
this.percentage = this.task.percentageChanges();
this.snapshot = this.task.snapshotChanges();
// The file's download URL
observableList.push(
this.task.snapshotChanges().pipe(
finalize(async () => {
return await fileRef.getDownloadURL();
}))
);
// observableList = this.task.snapshotChanges();
// observableList.push(taskObservable);
}
console.log(observableList);
forkJoin(
observableList
).subscribe(
response => {
console.log(response);
}
);
}
}
这部分:
this.task.snapshotChanges().pipe(
finalize(async () => {
return await fileRef.getDownloadURL();
}))
当我单独使用这个功能并像这样使用时:
this.task.snapshotChanges().pipe(
finalize(async () => {
this.downloadUrl = await fileRef.getDownloadURL().toPromise;
}))
他们返回正确的 URL,this.downloadUrl 是一个全局变量,即 downloadURL: Observable;
但我不想一个一个返回我想要3个结果,所以我有一个想法使用forkJoin,就像javascript中promise的Promise.All():
console.log(observableList);
forkJoin(
observableList
).subscribe(
response => {
console.log(response);
}
);
我在控制台中得到了这个:
(3) [UploadTaskSnapshot、UploadTaskSnapshot、UploadTaskSnapshot]
我如何从他们那里获得下载 URL?
【问题讨论】:
标签: javascript angular firebase firebase-storage