如何上传CSV文件并使用angular2读取它们? [英] How to upload a CSV file and read them using angular2?
问题描述
我需要上传.CSV文件并在我的组件中读取它们,我浏览了此博客,但是它在特定位置存储了.CSV文件,我想上传.CSV文件并阅读它们在我的组件中.我该怎么办
i have a requirement of uploading a .CSV file and read them inside my component, i have gone through this blog but it has a .CSV file stored in a particular loaction, i want to upload the .CSV file and read them inside my component. How can i do it
我们有任何可以使用的内置插件吗?如果没有,那么我们如何才能实现这一目标.
Do we have any build in plugin which we can use? if not then how can we achieve this goal.
这是我尝试过的代码
视图
<input type="file" name="File Upload" id="txtFileUpload"
(change)="changeListener($event)"
accept=".csv"/>
组件
changeListener($event:Response): void {
debugger;
// i am not able to get the data in the CSV file
}
在changeListener()中,我无法获取.CSV文件的内容,有人可以帮我吗?
in the changeListener(), i am not able tom get the content of the .CSV file, can anyone help me on this?
谢谢
推荐答案
将csv文件上传到某个位置并获取csv文件数据.请尝试以下操作:-
Upload your csv file to a location and get the csv file data. Please try this:-
Component.html:-
<input type="file" class="upload" (change)="changeListener($event.target.files)">
Component.ts:-
public changeListener(files: FileList){
console.log(files);
if(files && files.length > 0) {
let file : File = files.item(0);
console.log(file.name);
console.log(file.size);
console.log(file.type);
let reader: FileReader = new FileReader();
reader.readAsText(file);
reader.onload = (e) => {
let csv: string = reader.result as string;
console.log(csv);
}
}
}
这篇关于如何上传CSV文件并使用angular2读取它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!