Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 File API: FileReader.readAsText() returns "undefined"

I use Chrome 12 on Mac OS X and I've included jQuery 1.6.1 within the document.

I try to read the contents of a file as text and save it in a data-object with the following function:

this.upload = function(file) {
    console.log('FileHandler.upload called with ' + file.name + '.');
    console.log(file);
    console.log(this.reader);

    data = {
        content: this.reader.readAsText(file)
    }

    console.log('Content: ' + data.content);
}

"file" seams to be a valid file-object and "this.reader" is a fresh instance of type FileReader. This code creates the following console output:

http://cl.ly/1Y2b383G2F272x1m1P0N

enter image description here

like image 890
Claudio Albertin Avatar asked Jun 20 '11 17:06

Claudio Albertin


People also ask

What does the readAsText () function return?

The readAsText() method is used to read the contents of the specified Blob or File . When the read operation is complete, the readyState is changed to DONE , the loadend event is triggered, and the result property contains the contents of the file as a text string.


1 Answers

That's not the way it works according to the docs. You should call the readAsText() function, and when it's completed the result is stored in .result.

like image 156
pimvdb Avatar answered Oct 14 '22 18:10

pimvdb