Coder
Coder

Reputation: 3130

FileReader: Object is not a function

My code -

$selectFile = $('<input type="file">');

$selectFile.click(function () {
            this.value = null;
});      
$selectFile.change(function(event){
    var reader = new FileReader();
    reader.readAsDataURL(event.target.files[0]);
    reader.onloadend(function(e){
        alert(e.result);
    });                           
});

I am getting object is not a function error at reader.onloadend line.

Can anyone help please.

Upvotes: 0

Views: 4733

Answers (1)

Adam Merrifield
Adam Merrifield

Reputation: 10407

You need to change your onloadend method to onload. Also you need to set it to a function, not a parameter. To get the url/data you want use e.target.result not just e.result. Finally, I'd place the readAsDataURL after setting the method to make sure that it fires.

$selectFile.change(function(event){
    var reader = new FileReader();
    reader.onload = function(e){
        alert(e.target.result);
    };             
    reader.readAsDataURL(event.target.files[0]);              
});

DEMO

Upvotes: 1

Related Questions