Error reading binary file

Asked

Viewed 82 times

1

I am trying to read a binary (digital biometric) file that the user selects. But it is not returning anything.

What is wrong?

Follows code below:

var fileInput = document.getElementById('fileInput');
var file = fileInput.files[0];
var reader = new FileReader();
var campo = "";
reader.onload = function(e) {
     campo = reader.result;
}
reader.readAsArrayBuffer(file);
document.getElementById('template').value = campo;
alert("CAMPO TAMANHO --> " + campo.length);
}
  • You have a } more in your code. That’s just like it or it was from copy/Paste?

1 answer

2

Without having the complete code to run, it’s hard to guess exactly what’s going wrong.

That being said, one thing that is strange about your code is that you are reading the value of the field without waiting for the onload to run first. Not waiting for callback when the code is asynchronous is a very common error.

Try to put the code that uses the result inside the callback:

reader.onload = function(e) {
    var campo = reader.result;
    document.getElementById('template').value = campo;
    alert("CAMPO TAMANHO --> " + campo.length);
}
reader.readAsArrayBuffer(file);

Browser other questions tagged

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