使用get请求下载文件非常简便,但是get请求的url有长度和大小的限制,所以当请求参数非常多时无法满足需求,所以改成post请求
const res = await fetch('xxxxxxxxx', {
method: 'post',
body: JSON.stringify(params),
credentials: 'include',
headers: {
'Cache-Control': 'max-age=0',
'Pragma': 'no-cache',
'Content-Type': 'application/json;charset=UTF-8',
'x-requested-with': 'fetch'
}
});
const blob = await res.blob();
if ('download' in document.createElement('a')) {
var a = document.createElement('a');
a.style.display = 'none';
var url = window.URL.createObjectURL(blob);
var filename = decodeURIComponent(res.headers.get('Content-Disposition'));
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
} else {
navigator.msSaveBlob(blob);
}