ファイルをバイナリで取得することを考える。
const url = './sample.png';
const xhr = new XMLHttpRequest();
xhr.open( 'GET', url );
//データの受け取り方を指定
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
if ( this.status === 200 || this.status === 304 ) {
const data = this.response;
}
};
xhr.send();
responseType(responseの種別)で指定できるのは6種類。
- 空文字列(デフォルト)
textが指定された場合と同等。 - arraybuffer
物理メモリの領域(参照か?)。 - blob
Binary Large OBjectの略。バイナリデータ本体。 - document
xmlまたはhtml。 - json
json。 - text
text。
バイナリファイルを受け取るときはarraybufferかblobを指定する。