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">
It worked!!! Thanks.
– Lagaggio
Look at the line that gave the error.
– Sam
Actually gave the error: Cannot set Property 'onchange' of null in the first line. Sorry
– Lagaggio
If the script is before the input file, put the whole code inside this function:
document.addEventListener("DOMContentLoaded", function(){ código aqui });
– Sam
Should this function be in the Javascript file? I’m new at this stop
– Lagaggio
I updated the answer.
– Sam
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?
– Lagaggio
I think inside the.
– Sam
Blz thanks for the help
– Lagaggio