【发布时间】:2019-04-28 01:10:36
【问题描述】:
我正在尝试使用新的@angular/fire 5.1.0
使用 AngularFireStorage 查看上传到 firebase 的图像。
我以前可以在 angularfire2 中使用task.downloadURL()
"如果我错了请纠正我,但现在我必须使用afStorage.getDownloadURL()
请您帮助我正确设置 imageUrl。
import { AngularFireStorage, AngularFireStorageReference, AngularFireUploadTask } from '@angular/fire/storage'
....
downloadURL: Observable<string>;
imageUrl: string;
....
async onGetFromGallery(){
try{ ....
const imageData = await this.camera.getPicture(options);
const image = 'data:image/jpeg;base64,' + imageData;
const id = Math.random().toString(36).substring(2);
const user = this.authenticatorService.getUser();
this.ref = this.afStorage.ref(user.uid + '/images/categories/'+id);
this.task = this.ref.putString(image, 'data_url');
//--- Previously:angularfire2:
// this.downloadURL = this.ref.getDownloadURL();
// this.downloadURL.subscribe(url=> this.imageUrl=url);
//--- now replaced by this.ref.getDownloadURL()...
//My Attempt - unable to getDownloadUrl?
this.task
.snapshotChanges().toPromise().then(_ =>
{
this.ref.getDownloadURL().toPromise().then(res => {
console.log('URL: ', res);
this.imageUrl = res;
});
})
} catch(e) {
console.error(e);
this.errorMessage = JSON.stringify(e);
}
}
查看摘录:
<img [src]="imageUrl" style="width:100%;">
package.json 摘录:
"@angular/compiler-cli": "5.2.11",
"@angular/fire": "^5.1.0",
“火力基地”:“^5.5.9”,
"cordova-android": "~7.0.0",
谢谢。
【问题讨论】:
-
根据github.com/angular/angularfire2/blob/master/docs/storage/…,你应该只需要
this.imageUrl = ref.getDownloadURL()。 -
请查看https://stackoverflow.com/a/57267424/11127383 以了解此时它是如何工作的。
标签: javascript firebase firebase-storage angularfire2