//request.js
//对返回数据进行判断,(该项目是因为会对code进行响应拦截,文件流不返回code,所以对code不做判断)
// if ('application/octet-stream' == response.headers['content-type']) {
// return response.data
// }
download.js
import request from '@/utils/request'
function download(url, fileName, option) {
var fileName = (fileName || '文件下载').trim()
// request 函数是用 axios 封装的请求组件
return request({
url: url,
data: option.data || {},
method: option.method || 'get',
responseType: 'blob',
}).then((data) => {
saveFile(data, fileName + '.xlsx')
})
}
function saveFile(data, fileName) {
let blob = new Blob([data], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
}); // res就是接口返回的文件流了
let url = window.URL || window.webkitURL;
let fileURL = url.createObjectURL(blob);
let a = document.createElement("a");
a.href = fileURL;
a.download = fileName;
a.target = "_self";
a.click();
url.revokeObjectURL(fileURL);
}
export default download
student.js 调用
//导出表格
export function export_stu() {
return download('/students/export_stu',"学生基本信息列表",{'data':{},'method':'get'})
}