在XMLHttpRequest2里支持二进制数据的下载了,现分别以同步和异步两种方式分别介绍。
异步的方式下载:
xmlRequest.open("GET", "0.jpg", true);
xmlRequest.responseType = "blob";//这里是关键,它指明返回的数据的类型是二进制
xmlRequest.onreadystatechange = function(e) {
if (this.readyState == 4 && this.status == 200) {
var response = this.response;
img.src = window.URL.createObjectURL(response);
}
}
xmlRequest.send(null);
同步的方式下载:
xmlRequest.open("GET", "0.jpg", false); xmlRequest.overrideMimeType('text/plain; charset=x-user-defined');//这里是关键,不然 this.responseText;的长度不等于文件的长度 xmlRequest.onreadystatechange = function(e) { if (this.readyState == 4 && this.status