Read file line by line and collect strings

Asked

Viewed 158 times

3

I have these two functions in Javascript:

function verificaExtensao($input) {

    var extPermitidas = ['txt'];
    var extArquivo = $input.value.split('.').pop();

    if(typeof extPermitidas.find(function(ext){ return extArquivo == ext; }) == 'undefined') {
      alert('O arquivo não pode ser validado pois possui extenção não permitida!');
    } else {
      alert('Arquivo validado com sucesso!');
      doc();
    }
}

function doc(){

  document.getElementById('file').onchange = function(){
    var file = this.files[0];

    var reader = new FileReader();
    reader.onload = function(progressEvent){
      // Entire file
      console.log(this.result);

      // By lines
      var lines = this.result.split('\n');
      for(var line = 0; line < lines.length; line++){
        console.log(lines[line]);
      }
    };
    reader.readAsText(file);
  };
}

The one that checks the extension works normally, but the one that would show the file on the console does not show.

Another question I have would be when collecting the string, for example within the file I would have several lines and in each line would have several conditions, for example from position 2 until position 10 should be contained only numbers. How could I do that?

My HTML:

<imput type="file" onchange="verificaExtensao(this)" id="file"name="file">

1 answer

2


The code doesn’t work because you created an event change within the function doc() that does nothing the first time the function is called. And even if it worked, would error because you are not passing anything to function.

I believe you don’t even need two functions, tap use the function of the onchange and do everything within it. If the file is invalid, put a return after Alert. Hence you don’t need to use the attribute onchange in the input.

See how it gets simpler:

document.addEventListener("DOMContentLoaded", function(){
   document.getElementById('file').onchange = function(){

    var extPermitidas = ['txt'];
    var extArquivo = this.value.split('.').pop();

    if(typeof extPermitidas.find(function(ext){ return extArquivo == ext; }) == 'undefined') {
      alert('O arquivo não pode ser validado pois possui extenção não permitida!');
      return;
    } else {
      alert('Arquivo validado com sucesso!');
    }

    var file = this.files[0];

    var reader = new FileReader();
    reader.onload = function(progressEvent){
      // Entire file
      console.log(this.result);

      // By lines
      var lines = this.result.split('\n');
      for(var line = 0; line < lines.length; line++){
        console.log(lines[line]);
      }
    };
    reader.readAsText(file);
  }
});
<input type="file" id="file"name="file">

  • It worked!!! Thanks.

  • Look at the line that gave the error.

  • Actually gave the error: Cannot set Property 'onchange' of null in the first line. Sorry

  • 1

    If the script is before the input file, put the whole code inside this function: document.addEventListener("DOMContentLoaded", function(){ código aqui });

  • Should this function be in the Javascript file? I’m new at this stop

  • I updated the answer.

  • Good one worked!!! if I wanted for example to do conditionals to pick strings from each line, I should put these conditions after the 'for' of the lines?

  • 1

    I think inside the.

  • Blz thanks for the help

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.