这篇文章将为大家详细讲解有关vue项目中如何实现以blob形式导出文件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1、首先要确定服务器返回的数据类型。
在请求头中加入: config.responseType = 'blob'
有时候,不是所有接口都需要该类型,则可以对接口做一个判定:
// request拦截器
service.interceptors.request.use(
config => { // 根据接口判定
if ( config.url === '/setting/exportData' ||
config.url.indexOf('export') > -1 ||
config.url.indexOf('Export') > -1) {
config.responseType = 'blob' // 服务请求类型
}
if (getToken()) {
config.headers['access_token'] = getToken()
}
return config
},
error => {
// Do something with request error
// console.log(error) // for debug
Promise.reject(error)
}
)
2、接口请求获取后端返回的文件流
// 导出
onExport() {
if (this.dataList === '') {
this.$message({
type: 'error',
message: '暂无数据导出'
})
return
}
const fd = new FormData()
fd.append('id', this.id)
var exportFileName = '导出文件名' //设置导出的文件名,可以拼接一个随机值
exportData(fd)
.then(res => {
// res.data 是后端返回的文件流
// 调用 downloadUrl 处理文件
downloadUrl(res.data, exportFileName)
})
.catch(err => {
this.$message({
type: 'error',
message: err.message
})
})
},
3、文件处理downloadUrl--该方法可以写为公共方法以便调用
// 使用iframe框架下载文件--以excel为例,可修改type与fileName选择文件类型
export function downloadUrl(res, name) {
const blob = new Blob([res], { type: 'application/vnd.ms-excel' }) // 构造一个blob对象来处理数据
const fileName = name + '.xlsx' // 导出文件名
const elink = document.createElement('a') // 创建a标签
elink.download = fileName // a标签添加属性
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click() // 执行下载
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink) // 释放标签
}
4、在ie浏览器中存在兼容性问题,对downloadUrl做一些调整
// 使用iframe框架下载文件 -兼容性考虑
export function downloadUrl(res, name) {
const blob = new Blob([res], { type: 'application/vnd.ms-excel' })
// for IE
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
const fileName = name + '.xlsx'
window.navigator.msSaveOrOpenBlob(blob, fileName)
} else {
// for Non-IE (chrome, firefox etc.)
const fileName = name + '.xlsx'
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href)
document.body.removeChild(elink)
}
}
总结:至此,以文件流的形式导出文件的一种方式便已经实现。
补充知识:vue中使用文件流进行下载(new Blob),不打开一个新页面下载
我就废话不多说了,大家还是直接看代码吧~
export function download (url, params, filename) {
Message.warning('导出数据中')
return axios.get(url, {
params: params,
responseType:'arraybuffer',
}).then((r) => {
const content = r.data
const blob = new Blob([content],{type:'application/vnd.ms-excel'})
if ('download' in document.createElement('a')) {
const elink = document.createElement('a')
elink.download = filename
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href)
document.body.removeChild(elink)
Message.success('导出成功')
}
}).catch((r) => {
console.error(r)
Message.error('导出失败')
})
}
关于vue项目中如何实现以blob形式导出文件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。