AJ Goudel
AJ Goudel

Reputation: 349

How to get the text from a csv file in vanilla javascript

I have an input tag: <input type="file" id="fileUpload" accept=".csv"/> that gets a csv file from the user. I then store the file into a variable as such: const csvFile = document.getElementById('fileUpload'); How can I get the contents of the file into one big string if possible?

Upvotes: 0

Views: 806

Answers (1)

Abid Hasan
Abid Hasan

Reputation: 658

You can use the FileReader to read files.

<input type="file" id="fileUpload" accept=".csv" onchange="open(event)" />

<script>
  var open = function(event) {
    var input = event.target.files[0]

    var readerObj = new FileReader()

    readerObj.onload = function() {
      var fileText = readerObj.result
      //do something with fileText here....
    }
    readerObj.readAsText(input)
  }
</script>

Upvotes: 2

Related Questions