admin管理员组文章数量:1391929
I'm using KnockoutJS in my MVc Project to load a csv file & provide a simple validation for import mechanism
The workflow is as follows:
- User selects a CSV file (sample provided).
- User clicks a button (weehoo...).
- Client-side code takes the CSV, parses it, and loads it into KnockoutJS array. All is working Perfect i can upload the file but the problem that my Code Load The Empty line in csv files, I dont want that the user remove the empty lines manual before importing the file
this is an example of csv Lines :
Account_id,External_id,Amount,Transaction_Date,Office_date,Bank,Receipt_nbr,Type,statement,receipt,
0559394,,5,6/20/2017,7/7/2017,Cash,1729002903597,PNL,172900290,3597,
0099952,,19,6/20/2017,7/7/2017,Cash,1729002903653,PNL,172900290,3653,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
Here my code used to Upload file :
$('#lnkUpload').click(function () {
var FileToRead = document.getElementById('UserFile');
if (FileToRead.files.length > 0) {
var reader = new FileReader();
// assign function to the OnLoad event of the FileReader
// non synchronous event, therefore assign to other method
reader.onload = Load_CSVData;
// call the reader to capture the file
reader.readAsText(FileToRead.files.item(0));
}
self.userModel.removeAll();
});
function Load_CSVData(e) {
self.userModel.removeAll();
CSVLines = e.target.result.split(/\r\n|\n/);
$CSVLines = CSVLines.slice(1);
$.each($CSVLines, function (i, item) {
var element = item.split(","); // builds an array from ma delimited items
var LAccount_id = (element[0] == undefined) ? "" : element[0].trim();
var LExternal_id = (element[1] == undefined) ? "" : element[1].trim();
var LAmount = (element[2] == undefined) ? "" : element[2].trim();
var LTransaction_date = (element[3] == undefined) ? "" : element[3].trim();
var LOffice_date = (element[4] == undefined) ? "" : element[4].trim();
var LBank = (element[5] == undefined) ? "" : element[5].trim();
var LReceipt_nbr = (element[6] == undefined) ? "" : element[6].trim();
var LType = (element[7] == undefined) ? "" : element[7].trim();
var Lstatement = (element[8] == undefined) ? "" : element[8].trim();
var Lreceipt = (element[9] == undefined) ? "" : element[9].trim();
self.userModel.push(new userModel()
.Account_id(LAccount_id)
.External_id(LExternal_id)
.Amount(LAmount)
.Transaction_date(LTransaction_date)
.Office_date(LOffice_date)
.Bank(LBank)
.Receipt_nbr(LReceipt_nbr)
.Type(LType)
.statement(Lstatement)
.receipt(Lreceipt))
});
}
how can i update the code to Ignore and skip over empty Lines or treat them as the end of the input file or any other suggestion
I'm using KnockoutJS in my MVc Project to load a csv file & provide a simple validation for import mechanism
The workflow is as follows:
- User selects a CSV file (sample provided).
- User clicks a button (weehoo...).
- Client-side code takes the CSV, parses it, and loads it into KnockoutJS array. All is working Perfect i can upload the file but the problem that my Code Load The Empty line in csv files, I dont want that the user remove the empty lines manual before importing the file
this is an example of csv Lines :
Account_id,External_id,Amount,Transaction_Date,Office_date,Bank,Receipt_nbr,Type,statement,receipt,
0559394,,5,6/20/2017,7/7/2017,Cash,1729002903597,PNL,172900290,3597,
0099952,,19,6/20/2017,7/7/2017,Cash,1729002903653,PNL,172900290,3653,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
Here my code used to Upload file :
$('#lnkUpload').click(function () {
var FileToRead = document.getElementById('UserFile');
if (FileToRead.files.length > 0) {
var reader = new FileReader();
// assign function to the OnLoad event of the FileReader
// non synchronous event, therefore assign to other method
reader.onload = Load_CSVData;
// call the reader to capture the file
reader.readAsText(FileToRead.files.item(0));
}
self.userModel.removeAll();
});
function Load_CSVData(e) {
self.userModel.removeAll();
CSVLines = e.target.result.split(/\r\n|\n/);
$CSVLines = CSVLines.slice(1);
$.each($CSVLines, function (i, item) {
var element = item.split(","); // builds an array from ma delimited items
var LAccount_id = (element[0] == undefined) ? "" : element[0].trim();
var LExternal_id = (element[1] == undefined) ? "" : element[1].trim();
var LAmount = (element[2] == undefined) ? "" : element[2].trim();
var LTransaction_date = (element[3] == undefined) ? "" : element[3].trim();
var LOffice_date = (element[4] == undefined) ? "" : element[4].trim();
var LBank = (element[5] == undefined) ? "" : element[5].trim();
var LReceipt_nbr = (element[6] == undefined) ? "" : element[6].trim();
var LType = (element[7] == undefined) ? "" : element[7].trim();
var Lstatement = (element[8] == undefined) ? "" : element[8].trim();
var Lreceipt = (element[9] == undefined) ? "" : element[9].trim();
self.userModel.push(new userModel()
.Account_id(LAccount_id)
.External_id(LExternal_id)
.Amount(LAmount)
.Transaction_date(LTransaction_date)
.Office_date(LOffice_date)
.Bank(LBank)
.Receipt_nbr(LReceipt_nbr)
.Type(LType)
.statement(Lstatement)
.receipt(Lreceipt))
});
}
how can i update the code to Ignore and skip over empty Lines or treat them as the end of the input file or any other suggestion
Share Improve this question asked Jul 7, 2017 at 11:08 user3619254user3619254 591 silver badge11 bronze badges 1-
2
if (item.length <= 10) { ...
in the loop is one way to skip the ma-only or empty lines. – Alex K. Commented Jul 7, 2017 at 11:14
2 Answers
Reset to default 5You can detect if the current line is a newline character.
if(item == "\n") { continue; }
EDIT:
As matt pointed out, if the whitespace is made up of spaces the above solution will not work. Simply erase any spaces to detect if the row is made up of spaces.
if(item == "\n" || item.trim().length == 0) { continue; }
This will detect a line that consists only of mas and/or white space (so will match "\n", "", ",,,,,", ", , , , ," etc. but not ",,,,1,,,,,"
if (item.match(/^[,\s]*$/)) { continue; }
本文标签: aspnet mvchow to handles empty lines when reading CSV files in javascriptStack Overflow
版权声明:本文标题:asp.net mvc - how to handles empty lines when reading CSV files in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744708948a2621011.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论