통신 Javascript 를 사용하여 Binary File 읽기

황제낙엽 2010.09.29 12:53 조회 수 : 846 추천:55

sitelink1 http://nagoon97.wordpress.com/2008/04/06...sing-ajax/ 
sitelink2 http://www.heypage.com/nagoon97/BinFileR..._demo.html 
sitelink3  
sitelink4 http://okjungsoo.tistory.com/7953804 
extra_vars4  
extra_vars5  
extra_vars6  

nagoon97님의 Reading binary files using Ajax 포스트에서는 Ajax를 이용하여 바이너리 파일에 접근하는 방법을 소개하고 있습니다. 

  1. function BinFileReader(fileURL) {...} 을 호출합니다.
  2. 내부 변수들을 초기화 작업을 수행합니다.
  3. Browser의 userAgent를 체크하여서 IE인 경우에는 BinFileReaderImpl_IE를 호출하고 그 외의 경우에는 BinFileReaderImpl을 호출합니다.
  4. function BinFileReaderImpl(fileURL) {...} 에서는
    1. XMLHttpRequest를 생성한 다음에 mime type을 'text/plain; charset=x-user-defined' 설정한 다음에 file을 요청합니다.
      req.overrideMimeType('text/plain; charset=x-user-defined');

    2. 성공적으로 로딩한 다음에는 fileContents에 responseText를 저장하고
    3. fileSize를 responseText.length를 저장합니다.
    4. 이 때 readByteAt(index) 메소드를 생성하는데 charCodeAt(index) & 0xff 값을 리턴합니다.

  5. function BinFileReaderImpl_IE(fileURL) {...}에서는 (VB코드로 구현)
    1. VB 코드를 사용하여 XMLHttp Request를 생성한 다음에
    2. 헤더에 "Accept-Charset", "x-user-defined"로 설정한 후 file을 요청합니다.
    3. file 로딩이 완료된 다음에는 responseBody를 통하여 byteArray를 얻어옵니다.
    4. 이 byteArray를 Array()로 변환한 다음에 fileSize를 생성 및 readByteAt()를 생성합니다.
내부에는 filePointer (private)변수를 가지고 있어서 readString()을 호출하여 읽어들이는 경우 해당하는 읽어들이는 위치로 파일 포인터(위에서 얻어온 Array 또는 String에 접근하기 위한 index)를 변경 시킨 다음에 읽어들이게 됩니다.

제공되는 API는 다음과 같습니다.
  1. getFileSize() : 파일크기를 리턴
  2. getFilePointer() : 파일을 읽어들일 위치를 지정하는 내부 포인터의 값
  3. movePointerTo(iTo) : 포인터 위치 이동 (0 이상 fileSize 아래의 값으로 제한을 주는 것 외에는 값만 변경)
  4. movePointer(iDirection) : 읽어들이는 방향의 변경
  5. 그 외에 readNumber, readString, readUnicodeString을 사용하여 파일을 읽기 위해서 사용되는 메소드 제공
제공되는 예제에 BMP 파일의 헤더를 직접 읽어들여서 width/height/bits per pixel 값과 fileSize를 읽어들이는 것을 보실 수도 있습니다.
번호 제목 글쓴이 날짜 조회 수
126 Function.apply() 와 Function.call() file 황제낙엽 2011.10.07 455
125 String 에 trim() 함수 적용하기 황제낙엽 2011.08.28 468
124 javascript array contains method 황제낙엽 2011.08.19 580
123 daumopeneditor 황제낙엽 2011.07.18 463
122 object clone 황제낙엽 2011.07.08 2191
121 YUI Compressor in Java file 황제낙엽 2011.07.04 440
120 iframe auto resize (cross browsing) 황제낙엽 2011.05.13 1058
119 자바스크립트 정렬(javascript formatter) 도구 JavaScript Code Improver file 황제낙엽 2011.04.04 465
118 Why does this simple Javascript prototype not work in IE? 황제낙엽 2011.03.24 674
117 Defining classes and inheritance (클래스 정의와 상속) 황제낙엽 2011.03.24 735
116 User Agent 관련 Reference URL 황제낙엽 2011.02.22 453
115 각 브라우저 별 User Agent 정보 황제낙엽 2011.02.22 1255
114 History of User Agent 황제낙엽 2011.02.22 471
113 Navigator 객체란? 황제낙엽 2011.02.22 516
112 Understanding User-Agent Strings 황제낙엽 2011.02.22 542
111 User Agent 정보 모음 file 황제낙엽 2011.02.22 11430
110 ActiveX 설치 여부를 검사하는 스크립트 황제낙엽 2011.02.13 4488
109 [JavaScript Tutorials] Error handling in JavaScript using try/catch/finally - The Error object and throwing your own errors (해석중) 황제낙엽 2009.04.10 488
108 [JavaScript Tutorials] More leakage patterns (해석중) 황제낙엽 2009.04.10 484
107 [JavaScript Tutorials] Introducing the closure (해석중) 황제낙엽 2009.04.10 996