原生ajax 下载文件如excel等
var loins_excelDownload = function(option){
// var url = '/s1/brand/exportExcel';
var xhr = new XMLHttpRequest();
xhr.open(option.type, option.url, true);
xhr.responseType = "blob";
xhr.setRequestHeader("Content-Type",'application/json;charset=utf-8');
xhr.onload = function() {
if (this.status == 200) {
//var _b = xhr.getResponseHeader('Content-Disposition');
//var _c = _b.split('filename=')[1];
//var _d = decodeURIComponent(_c.split('.')[0])+'.'+_c.split('.')[1];
var blob = this.response;
var a = document.createElement('a');
var url = window.URL.createObjectURL(blob);//创建url对象
a.href = url;
// a.download = _d;
a.download = 'excel.xlsx';
a.click();
window.URL.revokeObjectURL(url);//释放url对象
}
}
xhr.send(JSON.stringify(option.data));
}
调用
loins_excelDownload({
type:'POST',
url:'/s1/brand/exportExcel',
data:{data:{brandCode: "1",brandEnName: "",brandName: "",createDate: "",endTime: "",flag: "",refCreateName: "",startTime: ""}}
});