10

I've got a drag and drop script that uses readAsArrayBuffer(). The length of the buffer is perfect, but I can't seem to figure out how to pull the data out of the buffer.

Apparently I've got to make a DataView or an Uint8Array or something, then iterate through its byteLength...help!

EDIT Pertinent code (there's not much of it):

var reader = new FileReader();
reader.onload = function(e) {
    // do something with e.target.result, which is an ArrayBuffer
} 
reader.readAsArrayBuffer(someFileHandle);
Ben
  • 54,723
  • 49
  • 178
  • 224
  • We might need a bit more code for this. Are you using a `FileReader`? Or what is providing `readAsArrayBuffer()`? – haylem Jun 08 '12 at 03:19

1 Answers1

3

This might change based on your answer to my comment, but if I assume that you are using a FileReader somewhere, you need to read it's result attribute in the loaded callback that you need to provide:

function loaded(evt) {  
  var datastring = evt.target.result;

  // do something here
}

reader.onload = loaded; // where reader is a FileReader, FileReaderSync 

Update: Ah, I see. Well then your best course of action is to follow to this duplicate:

Converting between strings and ArrayBuffers

Update2: Note that you could probably use readAsText() then, but I don't know if you're at liberty to do this.

Community
  • 1
  • 1
haylem
  • 22,460
  • 3
  • 67
  • 96
  • 1
    Yep, got that far thanks, but the `.result` property is of type `ArrayBuffer`. How can I extract the data from that? – Ben Jun 08 '12 at 03:24
  • @Steve: what do you mean by "extracting"? What do you want to do **exactly**? – haylem Jun 08 '12 at 03:26
  • Say the file contains a string: `foobarbaz`. I want something like `alert(datastring)` to alert `foobarbaz`. Right now, the alert says `[object ArrayBuffer]`. – Ben Jun 08 '12 at 03:27
  • @Steve: updated my answer, pointing you to another thread that provides what you need. – haylem Jun 08 '12 at 03:31
  • Thanks haylem. That's the one. Om. – Ben Jun 08 '12 at 03:32