vue+axios实现文件下载
https://blog.csdn.net/xjf106/article/details/89361311
https://www.cnblogs.com/yulj/p/8494465.html
// 响应拦截器 service.interceptors.response.use( /** * 通过接口返回码确定返回状态 * 还可以通过HTTP状态代码来判断请求状态 */ response => { // 响应数据 const res = response.data // 返回码200:成功 if (res.code === 200||res.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { return res } else { // 返回码401:AdminToken无效 if (res.code === 401) { MessageBox.confirm(res.msg, '提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => { store.dispatch('user/resetToken').then(() => { location.reload() }) }).catch(() => {}) } else { Message({ showClose: true, message: res.msg || 'Server error', type: 'error', duration: 5000 }) } return Promise.reject(new Error(res.msg || 'Server error')) } }, error => { // 响应错误 console.log(error.response) const res = error.response.data if (res.code === 401) { MessageBox.confirm(res.message, '提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => { store.dispatch('user/resetToken').then(() => { location.reload() }) }).catch(() => {}) } else { Message({ showClose: true, message: res.message || error.message, type: 'error', duration: 5000 }) } return Promise.reject(error) } )
export function download() { return request({ url: '/base/Teacher/download', method: 'get', responseType: 'blob' }) }
download() { download().then(response => { this.downloadFile(response); this.$message.success(response.msg) }).catch(function (error) { console.log(error); }); }, // 下载文件 downloadFile (data) { if (!data) { return } let blob = new Blob([data]) let fileName = '教师导入模板.xlsx' if ('download' in document.createElement('a')) { // 不是IE浏览器 let url = window.URL.createObjectURL(blob) let link = document.createElement('a') link.style.display = 'none' link.href = url link.setAttribute('download', fileName) document.body.appendChild(link) link.click() document.body.removeChild(link) // 下载完成移除元素 window.URL.revokeObjectURL(url) // 释放掉blob对象 } else { // IE 10+ window.navigator.msSaveBlob(blob, fileName) } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2016-05-18 git之https或http方式设置记住用户名和密码的方法
2013-05-18 office excel读写类NPOI
2007-05-18 发布一个Excel导入数据到GridView的类