Ajaxアクセスファイルのバイトストリームの取得方法

863 ワード

window.onload = function(){
    	
    	var xhr = new XMLHttpRequest();
    	xhr.open('GET', "http://localhost:8080/myblog/helloworld.pdf");
    	xhr.responseType = 'arraybuffer';
    	xhr.onreadystatechange = function getPdfOnreadystatechange(e) {
    		console.log("xhr.readyState:"+xhr.readyState+"   xhr.status:"+xhr.status);
    	    if (xhr.readyState === 4) {
    	      if (xhr.status === 200) {
    	        var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
    	                    xhr.responseArrayBuffer || xhr.response);
    	        console.log(data);
    	      } else {
    	        console.log("error");
    	      }
    	    }
    	};
    	xhr.send(null);
    }