easythrees
easythrees

Reputation: 1660

file reading event handler weirdness with javascript/html5

I'm trying to install an event handler in JavaScript to do some work on loading a file (I'm testing using Chrome). This is the Javascript I'm using:

// file reader...

function handleFileSelect(evt) 
{
    alert('event changed called!');
    var files = evt.target.files; // FileList object

    // files is a FileList of File objects. List some properties.
    var output = [];
    f = files[0];
    reader.readAsText(f);

    var reader = new FileReader();
    reader.onloadend = function(evt)
    {
        // alert(evt.target.result);
        alert('moo!');
    }
}

alert('adding event listener');
document.getElementById('files').addEventListener('change', handleFileSelect, false);  

The weird thing is that the alerts aren't firing, so I'm assuming that I've installed them incorrectly. I'm not sure what the mistake is though. Any ideas?

EDIT: Sorry, I should mention that it's the handleFileSelect(...) event handler that isn't firing.

Thank you for your time...

Upvotes: 0

Views: 171

Answers (1)

Musa
Musa

Reputation: 97717

You call reader.readAsText(f); before you assign reader to a value, so you get an error trying to call readAsText from an undefined value.

var reader = new FileReader();
reader.readAsText(f);

Upvotes: 2

Related Questions