前端根据后端返回的文件流实现下载功能
前段时间实现了一个根据后端接口实现文件下载功能,记录一下。
起初我请求后台数据返回的是这个样子的

需要对此进行操作
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 => {
let blob = res.data
let objectUrl = URL.createObject

本文介绍了如何在前端根据后端返回的文件流实现下载功能。通过在统一请求配置和下载页面请求函数中进行特定设置,成功触发文件的自动下载。
最低0.47元/天 解锁文章





