Skip to content

Instantly share code, notes, and snippets.

@mohayonao
Created February 12, 2013 12:23
Show Gist options
  • Save mohayonao/4761979 to your computer and use it in GitHub Desktop.
Save mohayonao/4761979 to your computer and use it in GitHub Desktop.
IE9でXMLHttpRequestを使ってバイナリファイルを読み込むやつ
var path = "";
var callback = function(array) {
console.log(array.length);
};
var xhr = new XMLHttpRequest();
xhr.open("GET", path);
xhr.responseType = "arraybuffer"; // IE9はこれが効かない.
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.response) {
// Chrome, Firefox, Safari, Opera はこれでOK
callback( new Uint8Array(xhr.response) );
} else if (xhr.responseBody !== undefined) { // なぜかundefinedと比較しないといけない.
// IE用. バイナリデータが配列に格納される.
callback( VBArray(xhr.responseBody).toArray() );
}
}
};
xhr.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment