axios前端请求下载接口怎么加加Content-Length标头
时间: 2023-12-27 16:05:07 浏览: 231
在axios发送请求时,可以通过设置请求头的方式添加Content-Length标头。下面是一个示例代码:
```
axios({
method: 'get',
url: '下载接口地址',
headers: {
'Content-Length': '0', // 设置Content-Length标头
},
responseType: 'blob', // 设置响应类型为blob类型,以便下载文件
})
.then(response => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', '文件名');
document.body.appendChild(link);
link.click();
})
.catch(error => {
console.log(error);
});
```
在上面的代码中,通过在请求头中添加Content-Length标头,设置响应类型为blob类型,以便下载文件。当服务器返回成功时,将文件转换为Blob对象,并创建一个下载链接,触发点击事件以便下载文件。
阅读全文
相关推荐


















