Vue-element-admin获取ThinkPHP5 传输过来的excel数据流的发方法
vue-element-admin
PanJiaChen/vue-element-admin: 是一个基于 Vue.js 和 Element UI 的后台管理系统模板,支持多种数据源和插件扩展。该项目提供了一个完整的后台管理系统模板,可以方便地实现后台管理系统的快速搭建和定制,同时支持多种数据源和插件扩展。
项目地址:https://gitcode.com/gh_mirrors/vu/vue-element-admin
·
api接口请求中添加responseType: 'arraybuffer',
export function exportData(data) {
return request({
url: '/crawler/taskmanagement/exportexcel',
responseType: 'arraybuffer',
method: 'post',
data
})
}
接受后端穿过来的数据流方法一:
DownloadData(id) {
var that = this
exportData({id: id}).then(res => {
let blob = new Blob([res.data], {type: "application/vnd.ms-excel"});
let objectUrl = URL.createObjectURL(blob);
window.location.href = objectUrl;
})
},
接受后端穿过来的数据流方法二(此方法可以指定下载表格的名称):
DownloadData(id) {
var that = this
exportData({id: id}).then(res => {
const filename = decodeURI(res.headers['content-disposition'].split(';')[1].split('=')[1]) || '分拣表.xlsx' // 获取后端返回存在请求头里边的表单名字
that.$options.methods.fileDownload(res.data, filename);
})
},
fileDownload(data, fileName) {
const blob = new Blob([data], {
type: 'application/octet-stream'
})
const filename = fileName || 'filename.xls'
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(blob, filename)
} else {
var blobURL = window.URL.createObjectURL(blob)
var tempLink = document.createElement('a')
tempLink.style.display = 'none'
tempLink.href = blobURL
tempLink.setAttribute('download', filename)
if (typeof tempLink.download === 'undefined') {
tempLink.setAttribute('target', '_blank')
}
document.body.appendChild(tempLink)
tempLink.click()
document.body.removeChild(tempLink)
window.URL.revokeObjectURL(blobURL)
}
},
后端使用ThinkPHP5,后端相关编码请查看https://blog.csdn.net/supramolecular/article/details/83303944
PanJiaChen/vue-element-admin: 是一个基于 Vue.js 和 Element UI 的后台管理系统模板,支持多种数据源和插件扩展。该项目提供了一个完整的后台管理系统模板,可以方便地实现后台管理系统的快速搭建和定制,同时支持多种数据源和插件扩展。
最近提交(Master分支:4 个月前 )
0caa975e - 3 年前
cd3f7267 - 3 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)