function downloadFunc(action, params) {
// 开始加载动画
setBusy();
var xhr =new XMLHttpRequest();
xhr.open('POST', action, true);
xhr.responseType ='arraybuffer';
xhr.onload =function () {
if (this.status ===200) {
var filename ="";
var disposition = xhr.getResponseHeader('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !==-1) {
var filenameRegex =/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches !=null&& matches[1])
// 文件名中含有中文,需要进行解码还原
filename =decodeURIComponent(matches[1].replace(/['"]/g, ''));
}
var type = xhr.getResponseHeader('Content-Type');
var blob =typeof File ==='function'?new File([this.response], filename, { type: type })
:new Blob([this.response], { type: type });
if (typeofwindow.navigator.msSaveBlob !=='undefined') {
// IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
window.navigator.msSaveBlob(blob, filename);
} else {
var URL =window.URL ||window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
if (filename) {
// use HTML5 a[download] attribute to specify filename
var a =document.createElement("a");
// safari doesn't support this yet
if (typeof a.download ==='undefined') {
window.location = downloadUrl;
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
}
} else {
window.location = downloadUrl;
}
setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
}
}
// 成功与否清除加载动画
clearBusy();
};
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send($.param(params));
}