前端根据后端返回的文件流实现下载功能
前段时间实现了一个根据后端接口实现文件下载功能,记录一下。
起初我请求后台数据返回的是这个样子的
需要对此进行操作
1,在统一请求的文件设置相关配置
基于封裝好的請求添加
headers: {
'Content-Type': 'application/json'
},
responseType: 'blob',
export function exportPerson(data) {
if (checkLogin())
return request({
url: 'real-name-building-business/supervisor/person/exportPerson',
method: 'post',
headers: {
'Content-Type': 'application/json'
},
responseType: 'blob',
data,
})
}
2.在需要下载的页面请求函数中
exportPerson(idList).then(res => {