【发布时间】:2019-03-18 09:22:25
【问题描述】:
我在逐行读取文本文件时遇到问题。使用 console.log(file) 效果很好,但我需要每个特定的行来处理它们,所以这是我到目前为止所做的。
在 api.service.ts 我有一个从服务器下载文件的函数,函数本身看起来像这样:
getFile(url: string): Observable<File> {
return this.httpClient.get<File>(url, {responseType: "text"});
}
然后在 app.component.ts 中定义私有 'resultFile: File' 字段并将传入文件分配给此变量
getFile() {
this.apiService.getFile('http://127.0.0.1:8000/media/results/MINERvA/CC0pi/v1.0/nuwro.txt').subscribe(file => {
this.resultFile = file;
console.log(this.resultFile);
});
}
正如我在使用 console.log() 打印 resultFile 的内容之前提到的,效果很好。文件格式正确(带有新行),但是当我遍历结果文件时
for (const line of resultFile){
console.log(line);
}
它打印每一个字符而不是每一行。我认为问题可能是 responseType: "text" 将内容转换为纯字符串,但我找不到任何解决方案。抱歉问了这么愚蠢的问题,但我以前从未使用过 JS/TS。
【问题讨论】:
标签: angular typescript file