当后端接口返回流文件,使用postman的send and download可以直接将文件下载,但是前端应该怎么写接口响应逻辑
exportHandler(data){
let self = this;
let params = {
name: data.name,
Id: data.id
};
//请求接口
axios({
method: 'get',
url:'/export/file',
params: params,
responseType: 'blob',//定义接口响应的格式,很重要
}).then(response => {
if (!response || !response.data) {
self.$message({
type: 'error',
message: "导出失败!"
});
return;
}
let blob = new Blob([response.data]);//response.data为后端传的流文件
let downloadFilename = data.name + ".zip";//设置导出的文件名
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
//兼容ie浏览器
window.navigator.msSaveOrOpenBlob(blob, downloadFilename)
}else {
//谷歌,火狐等浏览器
let url = window.URL.createObjectURL(blob);
let downloadElement = document.createElement("a");
downloadElement.style.display = "none";
downloadElement.href = url;
downloadElement.download = downloadFilename;
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);
window.URL.revokeObjectURL(url);
}
self.$message({type: "success", message: '导出成功!'});
}).catch(err => {
self.$message({
type: 'error',
message: "导出失败!"
});
})
}
内容参考了其他优秀博客,以上仅业务代码模拟