[英]Angular hot observables with RxJS: Changes on array are not emitted to subscribers
[英]RxJs multiple observables emitted from FileReader - collect as array
我正在構建一個Filedrop平台,並且在從FileReader API讀取Blob文件時遇到問題。 代碼流是:以可觀察的數組形式從上載服務中的用戶獲取文件,並從Blob生成base64字符串,以在瀏覽器中顯示。
讀者:
async getFileList(files: File[]) {
let src!: IStoryFile[];
for (const f of files) {
const blob = await this.readFileBlob(f)
src = [{ file: f, src: blob }]
}
this.uploadSrv.setFilesUpload(src);
}
readFileBlob(file: File): Promise<any> {
return new Promise((resolve, _) => {
const reader = new FileReader()
reader.onloadend = (e) => {
resolve(reader.result)
}
reader.readAsDataURL(file)
})
}
解決了論壇中的每個文件之后,它將在服務next
,該服務為DOM * ngFor |提供最終數組。 異步循環:
protected files$!: Observable<IStoryFile[]>;
...
this.files$ = this.uploadSrv.getFilesUpload$()
.pipe(
tap(val => console.log(val)),
)
預期的結果是什么:該函數將根據此接口發出所有對象的單個數組:
export interface IStoryFile {
file: File;
src?: string | ArrayBuffer | null;
}
您每次都在for-of
表達式中重新初始化src
。 將其更改為push
。
async getFileList(files: File[]) {
let src!: IStoryFile[] = []; // Initialize
for (const f of files) {
const blob = await this.readFileBlob(f)
src.push({ file: f, src: blob }); // Push new object
}
this.uploadSrv.setFilesUpload(src);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.