Error reading binary file

1

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

What's wrong?

Follow the 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);
}
    
asked by anonymous 17.05.2015 / 18:21

1 answer

2

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

That 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 common good 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);
    
17.05.2015 / 18:30