<script type="text/javascript">
axios({
method:‘post‘,
url: ‘url‘,
// 最好qs.stringify,不然接口易报错400
data: {},
// blob类文件对象
responseType: ‘blob‘
}).then(res => {
let blob = new Blob(res.data,{
// 若是excel的话 需要设置下面的type
type: ‘application/vnd.ms-excel‘
});
let link = document.createElement(‘a‘);
link.href = window.URL.createObjectURL(blob);
// 文件名
let filename = res.headers;
filename = filename[‘content-disposition‘];
if (filename && filename.length > 0) {
filename = filename.split(‘:‘)[1].split(‘filename=‘)[1];
} else{
filename = ‘写死的名字.exls‘
}
link.download = filename;
link.click()
})
</script>
注意,想要获取headers中content-disposition的数据,服务端要加一个:
context.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");