javascript - angularfire2 上传后显示图片
问题描述
使用Example Usage中的示例代码。我能够成功上传图像文件并显示它的链接。但是,我想在上传后立即在 img 标签中显示图像,而不是链接。我将如何做到这一点?我尝试过这样的事情无济于事:
profileUrl: Observable<string | null>;
....
const task = this.storage.upload(path, file);
const ref = this.storage.ref(path);
this.uploadPercent = task.percentageChanges();
console.log('Image uploaded!');
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = this.profileUrl = ref.getDownloadURL())
)
.subscribe();
在我的模板文件中:
<img [src]="profileUrl | async" />
解决方案
我想到了。您不能订阅快照,但可以订阅下载参考。
const task = this.storage.upload(path, file);
const ref = this.storage.ref(path);
this.uploadPercent = task.percentageChanges();
console.log('Image uploaded!');
task.snapshotChanges().pipe(
finalize(() => {
this.downloadURL = ref.getDownloadURL()
this.downloadURL.subscribe(url => (this.image = url));
})
)
.subscribe();
推荐阅读
- angular - 将按钮动态添加到垫表角材料
- javascript - 如何使用 javascript 或 lodash 用新对象替换对象?
- kubectl - Microk8s 远程与 kubectl:您必须登录到服务器(未经授权)
- forms - 如何控制 Apache Royale Jewel 表单上的指标差距?
- c# - 创建只读引用结构的集合
- python - Django ModuleNotFoundError:尝试使用 wsgi 服务器托管应用程序时没有名为“mysite.settings”的模块
- html - CSS:如何将一行文本向后换行,以便最终文本始终填满容器的整个宽度?
- r - 具有更多颜色渐变的 R 颜色散点图
- python - 哪些数据结构可以作为打包参数传递给 Python 函数?
- rabbitmq - 为什么我不能使用rabbitmq.config?