ajax 请求回来的 二进制文件流 ,怎么处理下载 ?
1、设置 xmlhttprequest 的 responsetype 为 blob
2、bolb 转 url对象
3、实现点击事件
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
// 必须设置为 blob
xhr.responseType = "blob";
xhr.onload = function () {
if (this.status == 200) {
var blob = this.response;
var a = document.createElement('a');
var aurl = window.URL.createObjectURL(blob);
a.href = aurl;
a.download = '*****.xls';
a.click();
}
}
xhr.send();