Html 如何在 AngularJS 中读取 csv 文件内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26353676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:51:47 来源:igfitidea点击:
How to read csv file content in AngularJS?
提问by Mark Vincent Osea
I want to read a csv file and get its content by using AngularJS and HTML5. I want to put the content of the csv file in $scope.
我想读取一个 csv 文件并使用 AngularJS 和 HTML5 获取其内容。我想将 csv 文件的内容放在 $scope 中。
I have this code in my html
我的 html 中有这段代码
<table align="center" >
<tr class="borderless">
<td>Choose .csv file</td>
<td><input type="file" name="readCsvFile" id="readCsvFile" accept=".csv"> </td>
<td><input type="button" value="Upload" ng-click="uploadCsvFile()"></td>
</tr>
</table>
回答by Marian Ban
with a custom directive:
使用自定义指令:
app.directive('fileReader', function() {
return {
scope: {
fileReader:"="
},
link: function(scope, element) {
$(element).on('change', function(changeEvent) {
var files = changeEvent.target.files;
if (files.length) {
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
scope.$apply(function () {
scope.fileReader = contents;
});
};
r.readAsText(files[0]);
}
});
}
};
});