0

Can anyone figure out why this declaration of a FileReader object returns undefined?

console.log("this is f " + f);

console.log(f);

var reader = new FileReader(); 
var ref = reader.readAsArrayBuffer(f);
console.log(ref);
console.log("the reader object is " + reader.length);//why does reader not have a length       property?

Here's my jsFiddle: http://jsfiddle.net/trkkazulu/6bV63/

Thanks,

J. Wells

1 Answers1

3

FileReader doesn't have a length property. You might want Blob#size (File — your f is a File instance — inherits from Blob).

Here's a link to an SO answer I did a while ago showing the size of a file using the File API, if that's what you're trying to get.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks! What i'm ultimately trying to get is a reference to an audio file that can be used by a "decode" function that i have. This fileReader object is just being used right now to sort the different audio formats for different decoders. – Jair-Rohm Parker Wells Mar 11 '13 at 11:05
  • I've changed "length" to "size" and the FileReader object still comes back as "undefined" and its size property doesn't exist. What am i doing wrong? – Jair-Rohm Parker Wells Mar 11 '13 at 11:06
  • @Jair-RohmParkerWells: Look at the answer again: The `size` property isn't on `FileReader`, it's on `Blob` (and inherited by `File`). So not `reader`, but `f`. More likely, though, given your comment above you probably want to respond to the `load` event and look at the data in the `ArrayBuffer` in [`reader.result`](http://www.w3.org/TR/FileAPI/#dfn-result). [Here's another SO answer](http://stackoverflow.com/questions/3146483/html5-file-api-read-as-text-and-binary/3146509#3146509) I've done showing actually getting the content of the file (you're interested in the "binary" bit). – T.J. Crowder Mar 11 '13 at 11:11
  • Ok, i moved my call for the reader object's size into the reader.onload() block. Now i get a size by calling 'size' on the 'file' parameter that i'm passing to reader.onload. Ok, cool. Still, my variable "stuff" comes back "undefined". – Jair-Rohm Parker Wells Mar 11 '13 at 11:12
  • @Jair-RohmParkerWells: There's no `stuff` variable in your question. (Questions shouldn't rely on linked-only content -- er, linked off-site that is, it's fine to rely on SO content.) But I think if you read through the two examples I've linked, particularly the second one, you'll get there. – T.J. Crowder Mar 11 '13 at 11:13
  • @ T.J. Crowder Very cool. Thanks. I liked to a JsFiddle of my project. I'll look at the SO that you sent. Thanks! – Jair-Rohm Parker Wells Mar 11 '13 at 11:23
  • this is "stuff"` if(extension === "amr") { 40 var stuff = e.target.result; 41 console.log("stuff is " + stuff.size + " bytes"); 42 console.log(stuff); 43` The funny thing is that 'stuff.size' comes back 'undefined' the call to "console.log(stuff)" prints the binary file out. What's up? – Jair-Rohm Parker Wells Mar 11 '13 at 11:27
  • @Jair-RohmParkerWells: Why are you looking at `e.target.result`? Sorry, but I don't think I can help you any further. Walk through the working examples I've provided, step by step, and you should be able to figure it out. – T.J. Crowder Mar 11 '13 at 11:55
  • @ T.J. Crowder I'll work through the examples you've sent. Thanks for your help. I appreciate it. – Jair-Rohm Parker Wells Mar 11 '13 at 22:02