[update] 下载excel文件功能的统一处理
This commit is contained in:
+60
-18
@@ -112,9 +112,48 @@ export function downFile(url,parameter){
|
|||||||
responseType: 'blob'
|
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 url 文件路径
|
||||||
* @param fileName 文件名
|
* @param fileName 文件名
|
||||||
* @param parameter
|
* @param parameter
|
||||||
@@ -122,23 +161,26 @@ export function downFile(url,parameter){
|
|||||||
*/
|
*/
|
||||||
export function downloadFile(url, fileName, parameter) {
|
export function downloadFile(url, fileName, parameter) {
|
||||||
return downFile(url, parameter).then((data) => {
|
return downFile(url, parameter).then((data) => {
|
||||||
if (!data || data.size === 0) {
|
// 调用a标签下载文件的方法
|
||||||
Vue.prototype['$message'].warning('文件下载失败')
|
downloadFileByA(data, fileName)
|
||||||
return
|
})
|
||||||
}
|
}
|
||||||
if (typeof window.navigator.msSaveBlob !== 'undefined') {
|
/**
|
||||||
window.navigator.msSaveBlob(new Blob([data]), fileName)
|
* post下载文件
|
||||||
} else {
|
* @param url 文件路径
|
||||||
let url = window.URL.createObjectURL(new Blob([data]))
|
* @param fileName 文件名
|
||||||
let link = document.createElement('a')
|
* @param parameter
|
||||||
link.style.display = 'none'
|
* @returns {*}
|
||||||
link.href = url
|
*/
|
||||||
link.setAttribute('download', fileName)
|
export function downloadPostFile(url, fileName, parameter) {
|
||||||
document.body.appendChild(link)
|
return axios({
|
||||||
link.click()
|
url: url,
|
||||||
document.body.removeChild(link) //下载完成移除元素
|
data: parameter,
|
||||||
window.URL.revokeObjectURL(url) //释放掉blob对象
|
method: 'post',
|
||||||
}
|
responseType: 'blob'
|
||||||
|
}).then((data) => {
|
||||||
|
// 调用a标签下载文件的方法
|
||||||
|
downloadFileByA(data, fileName)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-47
@@ -4,7 +4,7 @@
|
|||||||
* data中url定义 list为查询列表 delete为删除单条记录 deleteBatch为批量删除
|
* data中url定义 list为查询列表 delete为删除单条记录 deleteBatch为批量删除
|
||||||
*/
|
*/
|
||||||
import {filterObj} from '@/utils/util'
|
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 Vue from 'vue'
|
||||||
import {ACCESS_TOKEN, TENANT_ID} from '@/store/mutation-types'
|
import {ACCESS_TOKEN, TENANT_ID} from '@/store/mutation-types'
|
||||||
import store from '@/store'
|
import store from '@/store'
|
||||||
@@ -188,7 +188,7 @@ export const JeroListMixin = {
|
|||||||
that.$message.success(res.message)
|
that.$message.success(res.message)
|
||||||
// 判断当前删除的数据是否是最后一页的全部数据,如果是的话页码减一
|
// 判断当前删除的数据是否是最后一页的全部数据,如果是的话页码减一
|
||||||
if (that.ipagination.current > 1 && (that.ipagination.current === Math.ceil(that.ipagination.total / that.ipagination.pageSize)) &&
|
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
|
that.ipagination.current -= 1
|
||||||
}
|
}
|
||||||
@@ -282,25 +282,7 @@ export const JeroListMixin = {
|
|||||||
param['selections'] = this.selectedRowKeys.join(',')
|
param['selections'] = this.selectedRowKeys.join(',')
|
||||||
}
|
}
|
||||||
console.log('导出参数', param)
|
console.log('导出参数', param)
|
||||||
downFile(this.url.exportXlsUrl, param).then((data) => {
|
downloadFile(this.url.exportXlsUrl, fileName + '.xls', param)
|
||||||
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对象
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
/*
|
/*
|
||||||
* 下载模板
|
* 下载模板
|
||||||
@@ -309,25 +291,7 @@ export const JeroListMixin = {
|
|||||||
if (!fileName || typeof fileName != 'string') {
|
if (!fileName || typeof fileName != 'string') {
|
||||||
fileName = '模板文件'
|
fileName = '模板文件'
|
||||||
}
|
}
|
||||||
downFile(this.url.downTemplateUrl).then((data) => {
|
downloadFile(this.url.downTemplateUrl, fileName + '.xls')
|
||||||
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对象
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
/* 导入 */
|
/* 导入 */
|
||||||
handleImportExcel(info) {
|
handleImportExcel(info) {
|
||||||
@@ -343,12 +307,12 @@ export const JeroListMixin = {
|
|||||||
this.$warning({
|
this.$warning({
|
||||||
title: message,
|
title: message,
|
||||||
content: (<div>
|
content: (<div>
|
||||||
<span>{msg}</span>
|
<span>{msg}</span>
|
||||||
<br/>
|
<br/>
|
||||||
<span>具体详情请
|
<span>具体详情请
|
||||||
<a href={href} target="_blank" download={fileName}>点击下载</a>
|
<a href={href} target="_blank" download={fileName}>点击下载</a>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@@ -359,7 +323,7 @@ export const JeroListMixin = {
|
|||||||
const messageArr = info.file.response.message.split('</br>')
|
const messageArr = info.file.response.message.split('</br>')
|
||||||
let htmlDom = []
|
let htmlDom = []
|
||||||
messageArr.map(tt => {
|
messageArr.map(tt => {
|
||||||
if(tt) {
|
if (tt) {
|
||||||
htmlDom.push((<div>{tt}</div>))
|
htmlDom.push((<div>{tt}</div>))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -367,7 +331,7 @@ export const JeroListMixin = {
|
|||||||
title: '文件导入错误',
|
title: '文件导入错误',
|
||||||
content: (h) => <div>
|
content: (h) => <div>
|
||||||
{htmlDom}
|
{htmlDom}
|
||||||
</div>
|
</div>
|
||||||
})
|
})
|
||||||
// this.$message.error(`${info.file.name} ${info.file.response.message}.`);
|
// 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)
|
this.$refs.projectDetail.open(record)
|
||||||
},
|
},
|
||||||
/* 图片预览 */
|
/* 图片预览 */
|
||||||
|
|||||||
Reference in New Issue
Block a user