axios({
method: 'post',// 设置请求方式
url: `/modelapi/report/non-blocking`,// 设置请求地址
data: {// 参数
Id: this.activeTab,
ProjectId: this.projectId
},
headers: {// 设置请求头
ProjectId: this.projectId
},
responseType: 'blob'// 设置相应数据的类型,设置后后台返回的数据会被强制转为blob类型;如果后台返回代表失败的data,前端也无法得知,依然会下载得到名为undefined的文件。
}).then(function (res) {
// 得到请求到的数据后,对数据进行处理
let blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' });// 创建一个类文件对象:Blob对象表示一个不可变的、原始数据的类文件对象
let fileName = decodeURI(res.headers['content-disposition']);// 设置文件名称,decodeURI:可以对后端使用encodeURI() 函数编码过的 URI 进行解码。encodeURI() 是后端为了解决中文乱码问题
if (fileName){// 根据后端返回的数据处理文件名称
fileName = fileName.substring(fileName.indexOf('=') + 1);
}
const elink
axios请求文件流下载
最新推荐文章于 2024-05-09 14:46:33 发布
axios({ method: 'post',// 设置请求方式 url: `/modelapi/report/non-blocking`,// 设置请求地址 data: {// 参数 Id: this.activeTab, ProjectId: this.projectId }...
摘要由CSDN通过智能技术生成