Я хочу перебирать строку за строкой по загруженному пользователем файлу в моем приложении Angular. Я пробовал подход, изложенный в этом ответе, но я получаю следующая ошибка:
core.js: 6260 ОШИБКА TypeError: this.firstfile.split не является функцией или его возвращаемое значение не может быть повторено в AppComponent.firstfileupload (app.component.ts: 23) в AppComponent_Template_input_change
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) // } // ) } }
listener (app.component.html: 2) в executeListenerWithling (core.js: 21815) в wrapListenerIn_markDirtyAndPreventDefault (core.js: 21857) в HTMLInputElement. (platform-browser.js: 976) в ZoneDelegate.invokeTask (zone-evergreen.js: 399) в Object.onInvokeTask (core.js: 41640) в ZoneDelegate.invokeTask (zone-evergreen.js: 398) в Zone.runTask (zone-evergreen.js: 167) в ZoneTask.invokeTask [как вызываемый] (zone-evergreen.js: 480)
Мой код для 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)
// }
// )
}
}
И для 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>
Как я могу перебрать загруженный файл? Я бы предпочел не сохранять файл, а просто повторять только там. Заранее спасибо.