[update] 下载excel文件功能的统一处理
This commit is contained in:
+52
-10
@@ -112,20 +112,31 @@ export function downFile(url,parameter){
|
|||||||
responseType: 'blob'
|
responseType: 'blob'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载文件
|
* 通过A标签下载文件的通用方法
|
||||||
* @param url 文件路径
|
* @param data--接口返回的数据
|
||||||
* @param fileName 文件名
|
* @param fileName --下载文件的名称
|
||||||
* @param parameter
|
|
||||||
* @returns {*}
|
|
||||||
*/
|
*/
|
||||||
export function downloadFile(url, fileName, parameter) {
|
export function downloadFileByA(data, fileName) {
|
||||||
return downFile(url, parameter).then((data) => {
|
// 当没有返回数据或者返回错误信息的时候,对数据进行解析后提示
|
||||||
if (!data || data.size === 0) {
|
if (!data || data.type === 'application/json') {
|
||||||
Vue.prototype['$message'].warning('文件下载失败')
|
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
|
return
|
||||||
}
|
}
|
||||||
|
// if (!data || data.size === 0) {
|
||||||
|
// Vue.prototype['$message'].warning('文件下载失败')
|
||||||
|
// return
|
||||||
|
// }
|
||||||
if (typeof window.navigator.msSaveBlob !== 'undefined') {
|
if (typeof window.navigator.msSaveBlob !== 'undefined') {
|
||||||
window.navigator.msSaveBlob(new Blob([data]), fileName)
|
window.navigator.msSaveBlob(new Blob([data]), fileName)
|
||||||
} else {
|
} else {
|
||||||
@@ -139,6 +150,37 @@ export function downloadFile(url, fileName, parameter) {
|
|||||||
document.body.removeChild(link) //下载完成移除元素
|
document.body.removeChild(link) //下载完成移除元素
|
||||||
window.URL.revokeObjectURL(url) //释放掉blob对象
|
window.URL.revokeObjectURL(url) //释放掉blob对象
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get请求下载文件
|
||||||
|
* @param url 文件路径
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @param parameter
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
export function downloadFile(url, fileName, parameter) {
|
||||||
|
return downFile(url, parameter).then((data) => {
|
||||||
|
// 调用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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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'
|
||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user