Suppose I have a *.txt file with this content (4 lines of 5 elements each):

 1,2,3,4,5 6,7,8,9,10 11,12,13,14 15,16,17,18 

Is it possible to read this file using JS and make an array of this form from it: arr = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]]; ?
In PHP did this maneuver, but JS only investigating, and not everything is clear for now. Thank you in advance for your help!

    2 answers 2

    Implemented through FileReader

    HTML

     <input type="file" id="file" name="file" /> 

    Js:

     if ( ! (window.File && window.FileReader && window.FileList && window.Blob)) { alert('The File APIs are not fully supported in this browser.'); } function handleFileSelect(evt) { var file = evt.target.files[0]; if (!file.type.match('text.*')) { return alert(file.name + " is not a valid text file."); } var reader = new FileReader(); reader.readAsText(file); reader.onload = function (e) { var textToArray = reader.result.split("\n").map(function(x){return x.split(",")}); console.log(textToArray); }; } window.onload = function () { document.getElementById('file').addEventListener('change', handleFileSelect, false); } 

    Result (array):

     [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14],[15,16,17,18]]; 

    Demo: http://codepen.io/anon/pen/qZKQRx?editors=1011

      JavaScript does not have access to the client-side file system. This is done for security purposes. Therefore, the short answer is: no, it is impossible.

      See this question.

      • one
        But after all, through HTML5 API FileReader you can read a file loaded via input = file ...... accordingly, you can read the file - Alex Shimansky