export const exportFile = (pathname, params, data) => {
let name = (data && data.name) || 'data'
let fun = (data && data.fuc) || null
let url = `${baseUrl}${pathname}`
let xhr = new XMLHttpRequest();
if (!params) {
xhr.open('GET', url, true);
} else {
xhr.open('POST', url, true);
}
xhr.setRequestHeader('Authorization', `Bearer ${window.sessionStorage.token}`);
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhr.setRequestHeader('positionId', window.sessionStorage.uid ?
window.sessionStorage.posId :
window.sessionStorage.getItem('userInfo') && JSON.parse(window.sessionStorage.getItem('userInfo')).position.positionId)
xhr.responseType = "blob";
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let blob = this.response;
let reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = function (e) {
let appendFile = document.createElement('a');
appendFile.download = `${name}.zip`;
appendFile.href = e.target.result;
document.querySelector('body').append(appendFile);
appendFile.click();
appendFile.remove();
}
if (fun && typeof fun === 'function') {
fun('success')
}
} else {
console.log('读取附件失败')
if (fun && typeof fun === 'function') {
fun('err')
}
}
}
};
// 发送ajax请求
if (!params) {
xhr.send()
} else {
xhr.send(JSON.stringify(params))
}
}
导出文件 ()
最新推荐文章于 2023-06-02 14:06:20 发布