I need to loop through a user uploaded file line by line within my Angular application. I attempted the method mentioned in this solution but encountered the following issue:
core.js:6260 ERROR TypeError: this.firstfile.split is not a function or its return value is not iterable at AppComponent.firstfileupload (app.component.ts:23) at AppComponent_Template_input_change_2_listener (app.component.html:2) at executeListenerWithErrorHandling (core.js:21815) at wrapListenerIn_markDirtyAndPreventDefault (core.js:21857) at HTMLInputElement. (platform-browser.js:976) at ZoneDelegate.invokeTask (zone-evergreen.js:399) at Object.onInvokeTask (core.js:41640) at ZoneDelegate.invokeTask (zone-evergreen.js:398) at Zone.runTask (zone-evergreen.js:167) at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:480)
Here's the snippet from app.component.ts
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
firstfile=null;
second_file = null;
title = 'first';
constructor(private http:HttpClient){
}
firstfileupload(event){
console.log("First file")
console.log(event)
this.firstfile=event.target.files[0]
for(const line of this.firstfile.split(/[\r\n]+/)){
console.log(line)
}
console.log("First file File has been changed")
}
secondfile(event){
this.second_file=event.target.files[0];
// for(const line of this.second_file.split(/[\r\n]+/)){
// console.log(line)
// }
console.log("Second file uploaded")
}
onUpload(){
console.log("Upload button clicked")
// const fd = new FormData();
// fd.append('files',this.firstfile);
// fd.append('files',this.second_file);
// this.http.post('http://localhost:5000',fd).subscribe(res =>{
// console.log(res)
// }
// )
}
}
As well as app.component.html
<h1>Upload the files</h1>
<input type="file" (change)="firstfileupload($event)">
<input type="file" (change)="secondfile($event)">
<button type="button" (click)="onUpload()">Upload</button>
Is there a way to iterate over an uploaded file without saving it? Any help would be greatly appreciated.