2013-09-22 65 views
0

我在服務器上有一個二進制文件,我想讀取它。打開並讀取二進制文件(javascript)

我做類似的東西來獲取文件:

var request = new XMLHttpRequest(); 
request.open("GET", file); 
request.onreadystatechange = function() { 
    if (request.readyState == 4) { 
     doSomething(request.responseText); 
    } 
} 
request.send(); 

但之後,我真的不知道該怎麼做...什麼是做到這一點的正確方法?

有沒有辦法使用fileReader.readAsArrayBuffer()來做我想做的事情?

回答

3

設置的responseType到arrayBuffer像這樣:

var request = new XMLHttpRequest(); 
request.open("GET", file); 
request.responseType = 'arrayBuffer'; // the important part 
request.onreadystatechange = function() { 
    if (request.readyState == 4) { 
     doSomething(request.mozResponseArrayBuffer || request.response); // your arrayBuffer 
    } 
} 

request.send(); 
+0

不錯,簡單,完美! – user1278743