Struggling with reading text files line by line? While console.log(file) may work, it doesn't allow for processing each individual line. Here's my approach:
In api.service.ts, I've implemented a function to fetch the file from the server:
getFile(url: string): Observable<File> {
return this.httpClient.get<File>(url, {responseType: "text"});
}
Next, in app.component.ts, I declare a private 'resultFile: File' field and populate it with the downloaded 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);
});
}
While console.log() displays the content correctly, looping through resultFile outputs each character instead of each line. This could be due to responseType: "text" converting the data into plain strings.
for (const line of resultFile){
console.log(line);
}
I'm still searching for a solution to parse the text file line by line. Any suggestions are appreciated as I'm new to JS/TS programming!