Javascript函数,将从csv读取的总字节数放入变量中

时间:2013-01-27 15:49:17

标签: javascript ajax html5

我有一个本地存储在Web服务器上的CSV文件,它会定期添加所有不等长度的数字和字符串数据。我需要一种方法来添加每次读取的所有行字节值,因为从服务器读取文件的无状态特性,所以需要字节数。我需要保持下一个读取起始点!到目前为止,我可以得到每行的字节值,

            function renderData( data )
            {
                var dataRows = data.split( "\n" );
                var numrows = dataRows.length;
                var table = document.createElement("Table");
                table.border = 1;
                var nextindex = 0;
                for( i = 0; i < numrows; i++ )
                {
                    var rowlength = dataRows[i].length;
                    var totalBytes = numrows += rowlength;
                    currentindex = nextindex;
                    nextindex = totalBytes;
                    document.write(" Row "+i+" is " + rowlength + " bytes long ");
                    document.write(" next index starts " + totalBytes + " bytes in ");
                    var tableRow = table.insertRow(i);
                    var dataCells = dataRows[i].split(",");
                    for( j = 0; j <dataCells.length; j++ )
                    {
                        var tableCell= tableRow.insertCell(j);      
                        tableCell.innerHTML = dataCells[j];
                    }
                    executeSql( "INSERT INTO CAN ("", "", "", etc) values(?,?,?,)", dataCells );

                }
                document.body.appendChild( table );
            } 

1 个答案:

答案 0 :(得分:0)

如何而不是做var totalbytes = numrows*nextindex,使totalbytes成为一个全局变量并且每次迭代都进行totalbytes += rowlength;