【发布时间】:2020-09-03 06:24:19
【问题描述】:
我想在我的 Angular 应用程序中逐行迭代用户上传的文件。我已经尝试过answer 中所述的方法,但出现以下错误:
core.js:6260 ERROR TypeError: this.firstfile.split 不是函数 或者它的返回值是不可迭代的 在 AppComponent.firstfileupload (app.component.ts:23) 在 AppComponent_Template_input_change_2_listener (app.component.html:2) 在 executeListenerWithErrorHandling (core.js:21815) 在 wrapListenerIn_markDirtyAndPreventDefault (core.js:21857) 在 HTMLInputElement。 (平台-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>
如何迭代上传的文件?我宁愿不保存文件,只在那里迭代。 提前致谢。
【问题讨论】:
-
这能回答你的问题吗? Angular - Read a file and parse its content
-
我的问题有点不同。感谢反馈
-
答案是完全一样的......
-
我认为这里的答案比那里的答案更清晰,实现更好。但是,如果您仍然想关闭它,您可以。
标签: javascript angular typescript angular9