【发布时间】:2018-06-10 09:02:35
【问题描述】:
我正在使用 Angularfire2 上传和下载图片。但是在删除getDownloadURL() 之后,我就被困在这里了。
import { finalize } from 'rxjs/operators';
@Component({
selector: 'app-root',
template: `
<input type="file" (change)="uploadFile($event)" />
<div>{{ uploadPercent | async }}</div>
<a [href]="downloadURL | async">{{ downloadURL | async }}</a>
`
})
export class AppComponent {
uploadPercent: Observable<number>;
downloadURL: Observable<string>;
constructor(private storage: AngularFireStorage) {}
uploadFile(event) {
const file = event.target.files[0];
const filePath = 'name-your-file-path-here';
const fileRef = this.storage.ref(filePath);
const task = this.storage.upload(filePath, file);
// observe percentage changes
this.uploadPercent = task.percentageChanges();
// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = fileRef.getDownloadURL() )
)
.subscribe()
}
}
我在 HTML 页面中得到了downloadURL,但是如何进入函数内部呢?
【问题讨论】:
-
订阅 fileRef.getDownloadURL() 因为它返回一个 observable 并且你在函数中得到值。
标签: angular typescript firebase firebase-storage angularfire2