[update] 下载excel文件功能的统一处理

This commit is contained in:
fengchenchen
2021-09-07 20:28:20 +08:00
parent b8928cc2ba
commit f0389d2ab6
2 changed files with 71 additions and 65 deletions
+60 -18
View File
@@ -112,9 +112,48 @@ export function downFile(url,parameter){
responseType: 'blob'
})
}
/**
* 通过A标签下载文件的通用方法
* @param data--接口返回的数据
* @param fileName --下载文件的名称
*/
export function downloadFileByA(data, fileName) {
// 当没有返回数据或者返回错误信息的时候,对数据进行解析后提示
if (!data || data.type === 'application/json') {
const reader = new FileReader()
reader.readAsText(data)
// 处理load事件。该事件在读取操作完成时触发
reader.onload = e => {
// 解析返回的值
const res = JSON.parse(e.target.result)
// 异常信息抛出
if (!res.success) {
Vue.prototype['$message'].warning(res.message || '文件下载失败')
}
}
return
}
// if (!data || data.size === 0) {
// Vue.prototype['$message'].warning('文件下载失败')
// return
// }
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(new Blob([data]), fileName)
} else {
let url = window.URL.createObjectURL(new Blob([data]))
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对象
}
}
/**
* 下载文件
* get请求下载文件
* @param url 文件路径
* @param fileName 文件名
* @param parameter
@@ -122,23 +161,26 @@ export function downFile(url,parameter){
*/
export function downloadFile(url, fileName, parameter) {
return downFile(url, parameter).then((data) => {
if (!data || data.size === 0) {
Vue.prototype['$message'].warning('文件下载失败')
return
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(new Blob([data]), fileName)
} else {
let url = window.URL.createObjectURL(new Blob([data]))
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对象
}
// 调用a标签下载文件的方法
downloadFileByA(data, fileName)
})
}
/**
* post下载文件
* @param url 文件路径
* @param fileName 文件名
* @param parameter
* @returns {*}
*/
export function downloadPostFile(url, fileName, parameter) {
return axios({
url: url,
data: parameter,
method: 'post',
responseType: 'blob'
}).then((data) => {
// 调用a标签下载文件的方法
downloadFileByA(data, fileName)
})
}
+11 -47
View File
@@ -4,7 +4,7 @@
* data中url定义 list为查询列表 delete为删除单条记录 deleteBatch为批量删除
*/
import {filterObj} from '@/utils/util'
import {deleteAction, getAction, downFile, getFileAccessHttpUrl} from '@/api/manage'
import {getAction, downloadFile, getFileAccessHttpUrl} from '@/api/manage'
import Vue from 'vue'
import {ACCESS_TOKEN, TENANT_ID} from '@/store/mutation-types'
import store from '@/store'
@@ -188,7 +188,7 @@ export const JeroListMixin = {
that.$message.success(res.message)
// 判断当前删除的数据是否是最后一页的全部数据,如果是的话页码减一
if (that.ipagination.current > 1 && (that.ipagination.current === Math.ceil(that.ipagination.total / that.ipagination.pageSize)) &&
that.selectedRowKeys.length === that.dataSource.length
that.selectedRowKeys.length === that.dataSource.length
) {
that.ipagination.current -= 1
}
@@ -282,25 +282,7 @@ export const JeroListMixin = {
param['selections'] = this.selectedRowKeys.join(',')
}
console.log('导出参数', param)
downFile(this.url.exportXlsUrl, param).then((data) => {
if (!data) {
this.$message.warning('文件下载失败')
return
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(new Blob([data], {type: 'application/vnd.ms-excel'}), fileName + '.xls')
} else {
let url = window.URL.createObjectURL(new Blob([data], {type: 'application/vnd.ms-excel'}))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName + '.xls')
document.body.appendChild(link)
link.click()
document.body.removeChild(link) //下载完成移除元素
window.URL.revokeObjectURL(url) //释放掉blob对象
}
})
downloadFile(this.url.exportXlsUrl, fileName + '.xls', param)
},
/*
* 下载模板
@@ -309,25 +291,7 @@ export const JeroListMixin = {
if (!fileName || typeof fileName != 'string') {
fileName = '模板文件'
}
downFile(this.url.downTemplateUrl).then((data) => {
if (!data) {
this.$message.warning('文件下载失败')
return
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(new Blob([data], {type: 'application/vnd.ms-excel'}), fileName + '.xls')
} else {
let url = window.URL.createObjectURL(new Blob([data], {type: 'application/vnd.ms-excel'}))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName + '.xls')
document.body.appendChild(link)
link.click()
document.body.removeChild(link) //下载完成移除元素
window.URL.revokeObjectURL(url) //释放掉blob对象
}
})
downloadFile(this.url.downTemplateUrl, fileName + '.xls')
},
/* 导入 */
handleImportExcel(info) {
@@ -343,12 +307,12 @@ export const JeroListMixin = {
this.$warning({
title: message,
content: (<div>
<span>{msg}</span>
<br/>
<span>具体详情请
<span>{msg}</span>
<br/>
<span>具体详情请
<a href={href} target="_blank" download={fileName}>点击下载</a>
</span>
</div>
</div>
)
})
} else {
@@ -359,7 +323,7 @@ export const JeroListMixin = {
const messageArr = info.file.response.message.split('</br>')
let htmlDom = []
messageArr.map(tt => {
if(tt) {
if (tt) {
htmlDom.push((<div>{tt}</div>))
}
})
@@ -367,7 +331,7 @@ export const JeroListMixin = {
title: '文件导入错误',
content: (h) => <div>
{htmlDom}
</div>
</div>
})
// this.$message.error(`${info.file.name} ${info.file.response.message}.`);
}
@@ -397,7 +361,7 @@ export const JeroListMixin = {
/*
* 打开项目详情模态框
* */
openDetailModal(record){
openDetailModal(record) {
this.$refs.projectDetail.open(record)
},
/* 图片预览 */