1

I have files that I need to check if the first line in the CSV file has a header or not. If it does it will have the word 'symbol' first, if not it will not. So I am going to use this as the criteria for finding the header.

However using fileReader() I am not finding a way to do this? I am familiar with checking for objects however working with the file itself is eluding me.

There are tutorials online for using fileReader() but not that I found do this, can anyone provide some guidance?

1 Answer 1

2

You can use FileReader.prototype.readAsText(), String.prototype.split() with parameter \n, then check if first element in resulting array contains word "symbol"

document.querySelector("input[type=file]")
.onchange = function(e) {
  var reader = new FileReader();
  reader.onload = function(event) {
    var text = event.target.result;
    var arr = text.split(/\n/).filter(Boolean);
    if (/symbol/.test(arr[0])) {
      console.log("symbol found", arr[0]);
    }
  }
  reader.readAsText(e.target.files[0]);
}
<input type="file">

Sign up to request clarification or add additional context in comments.

1 Comment

@illcrx For example, you can download gist.githubusercontent.com/anonymous/… , then select file at stacksnippets to check if `"symbol" is found at header of file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.