1

I'm building a web application that requires some file validation and encoding checkups before uploading to the server. So, FileReader is great for this, but values in variables are not persisting, eventhough the variables are declared outside of every function: I have this code, works unitl testDesc function is reached:

var fileDesc1;
var fileDesc2;

window.onload = function () {
    checkFile();
    testDesc();

}//window onload

function checkFile() {
    //input type=file
    var file = fileInput.files[0];

    var reader = new FileReader();

    reader.onload= function (e) {
        var contents = reader.result;


        fileDesc1 = arrayToString(contents);
        fileDesc2 = discoverType2File(fileDesc1);

        ...


    }
    reader.readAsArrayBuffer(file);
}

After I get the file descriptions I need them available anywhere in my JS code, but that is not the case:

function testDesc(){

    //fileDesc1 is empty
    encode(fileDesc1);
    ...
    return validatyCheck
}

all functions are the in the same file yet the variables are empty. Why is this happening? I thought I knew JS, guess not. By the way all js files are loaded at the end of the html body tag.

lumee
  • 613
  • 1
  • 5
  • 15

0 Answers0