FileReader:获取文件名

FileReader: get file name

我有一个带有 Observable 的 FileReader,如下所示:

readDocument(fileChangeEvent: Event) {
    return new Observable<Object>(obs => {
        const file = (fileChangeEvent.target as HTMLInputElement).files[0];
        if (file) {
            const fileReader = new FileReader();
            fileReader.onload = (e) => {
                obs.next(fileReader.result);
            }
            fileReader.readAsText(file);
        }
    });
}

我在订阅中使用文件内容:

this.readDocument(document).subscribe(content => {

}

在订阅中,如何获取文件名?

像这样:

   this.readDocument(document).subscribe(content => {
        filename = content.getfilename;
    }

这可能吗?这里有什么解决方案?

您可以简单地 return 它在 Observable

readDocument(fileChangeEvent: Event) {
    return new Observable<any>(obs => {
        const file = (fileChangeEvent.target as HTMLInputElement).files[0];
        if (file) {
            const fileReader = new FileReader();
            fileReader.onload = (e) => {
                obs.next({
                   name: file.name,
                   result: fileReader.result,
                });
            }
            fileReader.readAsText(file);
        }
    });
}

Update Here is how i modified you update_chart() and it workz

update_chart(document) {
  this.readDocument(document).subscribe((file: any) => {
    console.log(file.name);
  });
}