[update] 下载excel文件功能的统一处理
This commit is contained in:
+60
-18
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user