2011-02-08 6 views
1

자바 스크립트에서 간단한 binaryStreamReader를 만들려고합니다.자바 스크립트/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가 반환하는 값을 반환하도록 할 수 있습니까? 에서 HTML5 파일 API를 조합 P :이 트릭을 할해야

+0

는 질문에 "자바 BinaryStreamReader"을 이름을 변경하고 "자바"로 태그를 다시하시기 바랍니다 :-) – rfw

+0

그것은 자바 스크립트입니다. –

+0

내가 "Java"라는 단어를 어딘가에서 본 것을 맹세한다 ... – rfw

답변

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); 
}); 

...

관련 문제