我有文本文件,除了我的第一行,我想抓住所有内容。另外,我想检查每行的列数。如何使用JavaScript File Reader完成这项工作?我使用代码bleow来读取第一行:
var fileExist = $('#fileUpload')[0];
var reader = new FileReader();
var file = fileExist.files[0];
reader.onload = function(e) {
var text = reader.result;
var firstLine = text.split('\n').shift();
var columnNames = firstLine.split('\t');
console.log(columnNames);
}
reader.readAsText(file, 'UTF-8');
答案 0 :(得分:2)
用pop()
摆脱第一行,然后遍历数组。
var fileExist = $('#fileUpload')[0];
var reader = new FileReader();
var file = fileExist.files[0];
reader.onload = function (e) {
var text = reader.result;
var allLines = text.split('\n');
// Print the colomn names
console.log(allLines.pop().split('\t'));
// Get rid of first line
allLines.pop();
// Print all the other lines
allLines.forEach(function (line) {
console.log(line.split('\t'));
});
}
reader.readAsText(file, 'UTF-8');