2017-02-10 98 views
0

我有了一个格式的多个数据的文本文件:制表符分隔文本文件数组中的JavaScript

1 2016-01-01 17:23:24 0 0 1 1 
2 2016-02-01 07:15:23 0 0 2 1 
1 2016-03-01 12:13:24 0 0 1 1 
3 2016-04-02 13:34:19 0 0 3 1 
..... 

代码:

<table> 
<tr> 
    <td colspan="2" style="font-weight:bold;"> 
    Upload time sheet for yards with biometrics 
    </td> 
</tr> 
<tr> 
    <td colspan="2" style="font-weight:bold;"> 
    <input type="file" name="fileUpload" id="fileUpload" size="40"> 
    </td> 
</tr> 
<tr> 
    <td colspan="2" > 
    &nbsp; 
    </td> 
</tr> 
<tr> 
    <th>Date From:</th><th>Date To:</th> 
</tr> 
<tr> 
    <td><input type="date" id="from_date"></td><td><input type="date" id="to_date"></td> 
</tr> 
    <td colspan="2" align="right"> 
    <input type="button" value="Process File" id="btn_process_file" onclick="Upload()"> 
    </td> 
</table> 

我需要在每行获得一定的值该文件被放置在一个数组中。我只需要数组中每行的前3个“值”。基于以上我的样本,我只需要:

1 2016-01-01 17:23:24 
2 2016-02-01 07:15:23 
1 2016-03-01 12:13:24 

为了使阵列,如:

var x = [[1,2016-01-01,17:23:24][2,2016-02-01,07:15:23][1,2016-03-01,12:13:24]] 
+0

var x = [[1,2016-01-01,17:23:24] [2,2016-02-01,07:15:23] [1,2016-03-01, 12:13:24]] '甚至没有有效的JS。请改善问题 – nozzleman

回答

0

你也可以使用文件,如<input type="file" accept='text/plain' onchange='readText(this)'/>

一个输入文件,这样你就能得到上述内容选定的文件通过使用文件阅读器的:

function readText(filePath) { 
     var output = ""; 
     if(filePath.files && filePath.files[0]) {   
      reader.onload = function (e) { 
       output = e.target.result; 
       displayContents(output); 
      }; 
      reader.readAsText(filePath.files[0]); 
      var result = output.split("\t"); 
      console.log(result); 
     } 
} 


var result = output.split("\t"); 

splitts您的字符串的数组。除以标签。

您也可以在系统上的文件上创建GET-Request。使用jQuery将使它更容易一点

var file = "file://your path" 
var toread= new XMLHttpRequest(); 
toread.open("GET", file, false); 
toread.onreadystatechange = function() 
{ 
    if(toread.readyState === 4) 
    { 
     if(toread.status === 200 || toread.status == 0) 
     { 
      var allText = toread.responseText; 
     } 
    } 
} 
toread.send(null); 
var result = allText.split("\\n"); 

$.get('file://your path', function(allText) { 
    var result = (allText.split("\\n"); 
}, 'text') 
+0

感谢您的教训,但我可以把文件弄好。我的问题是读取文件并将某些列仅放入数组中。 – Ibanez1408

+0

给我第二个我会把它添加到awkser – osanger

+0

完成。玩的开心! – osanger

0

您可以通过拆分尝试从 '\ t' 内文是指

如果你不使用jquery,借此在文本文件中的标签。

+0

你能告诉我如何?还需要将它放入一个数组中。 – Ibanez1408

+0

你想在jQuery或PHP中做到这一点? –

+0

Javascript请。 – Ibanez1408

-1
function Upload(){ 
    var files = document.getElementById('fileUpload').files; 

    if(files.length){ 
     var file = files[0]; // first 
     var reader = new FileReader(); 
     reader.onload = onReadFile; 
     reader.onerror = onError; 
     reader.readAsText(file); 
    } 
} 

function onReadFile(file){ 
    var txt = file.target.result; 
    var list = txt.split('\n'); 
    var result = list.map(function(item){ 
     return item.split(' '); 
    }); 

    console.log(result); 
} 


function onError(){ 
    //**// 
} 
0

你可以这样使用它。

 $.get('file://your path', function(allText) { 
    var result = allText.split("\t"); 
    //and access them with index value like this 
    alert(result.1); 
}); 
相关问题