import axios from 'axios'
import { message } from 'ant-design-vue'
const getFile = ({ url, params, tail, type }) => {
let data = type ? 'data' : 'params'
type = type || 'get'
return new Promise((resolve, reject) => {
axios({
headers: {
'hello': 'world'
},
withCredentials: true,
url,
method: type,
[`${data}`]: params,
responseType: 'blob'
}).then(res => {
console.log('export_file got res === ', res)
let resHeaders = res.headers
let resFileName = resHeaders['content-disposition'] || ''
let fileName = decodeURIComponent(resFileName.match(/filename\*\=(.*)/)[1]).split("utf-8''")[1]
const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
const a = document.createElement('a')
a.href = URL.createObjectURL(blob)
a.download = `${fileName}${tail}`
a.click()
URL.revokeObjectURL(a.href)
a.remove()
}).catch(err => {
console.log('export_file catch error === ', err)
message.error('文件导出失败')
reject(`faild: ${err}`)
})
})
}
export default getFile
vue如何处理文件流,下载excel文件
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 要求导出excel文件。当点击下载模板或下载反馈结果,axios发起后端接口请求,返回的数据获取 response...
- 这里介绍两种方法,使用 Blob对象 和 使用 js-file-download这两种方法下载的文件都不会乱码,但...
- 以前的文件下载可以直接通过a标签链接跳转,或者window.open()等都是打开页面方式直接处理。但这次的vue...
- 原文链接:https://blog.csdn.net/samarket/java/article/details/...