2011-02-08 61 views
1

我想在javascript中创建一个简单的binaryStreamReader。目前,我有以下几点:Javascript/HTML5试图创建一个BinaryStreamReader

function BinaryStreamReader(file){ 
    var reader = new FileReader(); 


    this.readBytes = function(start, bytes){ 

     reader.readAsBinaryString(file.slice(start,bytes)); 

     reader.onload = function(e){ 
      return e.target.result; //<-- this doesn't work now :P 
     } 

     reader.onerror = function(e){ 
      alert("FileError: " + e.target.error.code); 
     } 
    } 
} 

不过,我想用它像这样

var bsr = new BinaryStreamReader(); 
var data = bsr.readBytes(0,128); 

显然的ReadBytes()是不是在我的课返回任何东西。是否有可能让它返回onLoad返回的任何内容?

+0

请重命名你的问题的“Java BinaryStreamReader”,并重新标记为“Java”的:-) – rfw 2011-02-08 09:18:06

回答

1
 
function BinaryStreamReader(file){ 
    var reader = new FileReader(); 

    this.callback = null; 

    this.ready = function(callback) { 
     if(callback) { 
      this.callback = callback; 
     } 
    } 

    this.readBytes = function(start, bytes){ 

     reader.readAsBinaryString(file.slice(start,bytes)); 

     reader.onload = function(e){ 

      if(this.callback !== null) { 
       this.callback(e.target.result); 
      } 
     } 

     reader.onerror = function(e){ 
      alert("FileError: " + e.target.error.code); 
     } 
     return this; 
    } 
} 

var bsr = new BinaryStreamReader(); 
var data = bsr.readBytes(0,128).ready(function(data) { 
    alert(data); 
}); 

这应该做的伎俩......