1

I'm working in AngularJs the goal is to upload a file and save it as an a array using JS.

here is my HTML :

<input type="file" id = "uploadFile" name = "uploadFile" accept=".csv" required />
<button ng-click="processUploadedFile()"
        <div class="ml20 large-font">Process uploaded file</div>
</button>

Here is my function to process the uploaded file:

$scope.processUploadedFile = function() {
    var f = document.getElementById('uploadFile').files[0],
        r = new FileReader();

    r.onloadend = function(e) {
      var data = e.target.result;
    }

    r.readAsBinaryString(f);
}

It is showing the result is NULL.

And my CSV file would be like this:

Header1   Header2    Header3

value1     value2     value3
value1     value2     value3
value1     value2     value3
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Mar1009
  • 721
  • 1
  • 11
  • 27
  • Note that the [`readAsBinaryString`](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsBinaryString) method was once removed from the File API specification, but re-introduced for backward compatibility. Its use is not recommended. – georgeawg Nov 23 '19 at 21:04

1 Answers1

-1

Yes, you can. You just create a script that splits the data according with the relevant *.CSV delimiters, or get a third-party script that does that.

One such script is Papa Parse.

You can follow the example at: https://www.js-tutorials.com/javascript-tutorial/reading-csv-file-using-javascript-html5/

McKabue
  • 2,076
  • 1
  • 19
  • 34