const downloadOpera = throttle(function(url, filename) {
let xhr = new XMLHttpRequest();
xhr.open('GET', url,true);
xhr.setRequestHeader("Authorization", 'Bearer ' + token.value);
xhr.responseType = 'blob';
xhr.onload = function(e) {
//如果请求执行成功
if (this.status == 200) {
let blob = this.response;
let a = document.createElement('a');
let url = URL.createObjectURL(blob);
a.href = url;
if (filename) {
a.download = filename;
}
a.click();
window.URL.revokeObjectURL(url);
}
};
xhr.send();
}, 3000, {
leading: true,
trailing: false
})
// 下载
export function downloadOpera (url, params?) {
url = `${url}${params ? '?'.concat(qs.stringify(params)) : ''}`
let xhr = new XMLHttpRequest();
xhr.open('GET', url,true);
xhr.setRequestHeader("Authorization", 'Bearer ' + store.token);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (xhr.readyState === 4 && xhr.status === 200) {
let res = xhr.getAllResponseHeaders()
let result = res.split('\n')[1]
console.log(res)
console.log(result)
let name = result.split('filename=')[1].replace(/^\s*/, '').replace(/\s*$/, '')
let blob = xhr.response;
let a = document.createElement('a');
let url = URL.createObjectURL(blob);
a.href = url;
a.download = name
a.click();
window.URL.revokeObjectURL(url);
}
};
xhr.send();
}