2

I'm trying to read a text file of over 150,000 lines of text. I want to be able to read the text file and pass it as a parameter for processFileContent.

I tried it this way, but it doesn't work. Also, is there a better way to do this for such big data?

function readFile(file) {
  var reader = new FileReader();
  reader.onload = function (evt) {
    var data = evt.target.result;
};
  reader.readAsText(file);
  return data;
}

document.getElementById('file').addEventListener('change', readFile, false);

var data = readFile();

function processFileContent(data) {
  var list = data.split('\n');
  ...
3
  • "Also, is there a better way to do this for such big data?" Do you mean a "better" way to process the data? Commented Sep 18, 2016 at 3:20
  • Yes, a more efficient way perhaps? Commented Sep 18, 2016 at 3:42
  • Not certain what occurs within processFileContent?, though you should alternatively be able to read data in chunks of one to n lines ending in \n, instead of calling .split() for all of data at one call. Not certain how to measure "efficiency"? Compared to what other process? Commented Sep 18, 2016 at 3:47

2 Answers 2

2

FileReader.onload event returns results asynchronously. You can use a callback or Promise to return result of FileReader to processFileContent. Also file at readFile would be event object, not .files property of event.target.

function readFile(event) {
  var file = event.target.files[0];
  if (file) {
    new Promise(function(resolve, reject) {
      var reader = new FileReader();
      reader.onload = function (evt) {
        resolve(evt.target.result);
      };
      reader.readAsText(file);
      reader.onerror = reject;
    })
    .then(processFileContent)
    .catch(function(err) {
      console.log(err)
    });
  }
}

document.getElementById('file')
.addEventListener('change', readFile, false);

function processFileContent(data) {
  var list = data.split('\n');
  ...
Sign up to request clarification or add additional context in comments.

Comments

0

One of your problems is with scoping. You declared data as a local variable in the onload event handler, and tried returning it from the outer function, in which it was undefined. To fix this, you need to move the variable declaration out of the event handler.

You also are expecting a file as an argument to your event handler it seems, however events pass Event objects to their event handlers. To get the file, you need to get event.target.files[0]. This should fix it.

function readFile(event) {
    var file = event.target.files[0];  // getting the file Blob
    var reader = new FileReader();
    var data;  // moved declaration
    reader.onload = function (evt) {
        data = evt.target.result;
    };
    reader.readAsText(file);
    return data;
}

Comments

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.