Skip to content

Commit 5b36b62

Browse files
增加vue3版本通用导出方法
1 parent 2ce4e09 commit 5b36b62

File tree

2 files changed

+40
-2
lines changed
  • Vol.Vue3版本/src/api
  • 开发版dev/Vue.NetCore/Vol.Vue3版本/src/api

2 files changed

+40
-2
lines changed

Vol.Vue3版本/src/api/http.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ function get (url, param, loading, config) {
149149
}
150150

151151

152+
function download (url, params, fileName, loading,callback) {
153+
fileName = fileName.replace(">", ">").replace("<", "<");
154+
post(url, params, loading, { responseType: 'blob' }).then(content => {
155+
const blob = new Blob([content])
156+
if ('download' in document.createElement('a')) { // 非IE下载
157+
const elink = document.createElement('a')
158+
elink.download = fileName
159+
elink.style.display = 'none'
160+
elink.href = URL.createObjectURL(blob)
161+
document.body.appendChild(elink)
162+
elink.click()
163+
URL.revokeObjectURL(elink.href) // 释放URL 对象
164+
document.body.removeChild(elink)
165+
} else { // IE10+下载
166+
navigator.msSaveBlob(blob, fileName)
167+
}
168+
callback&&callback();
169+
})
170+
}
152171

153172

154173
function createXHR () {
@@ -293,4 +312,4 @@ ajax.post = function (url, param, success, errror) {
293312
ajax.get = function (url, param, success, errror) {
294313
ajax({ url: url, param: param, success: success, error: errror, type: 'get' })
295314
}
296-
export default { post, get, ajax, ipAddress }
315+
export default { post, get,download, ajax, ipAddress }

开发版dev/Vue.NetCore/Vol.Vue3版本/src/api/http.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ function get (url, param, loading, config) {
149149
}
150150

151151

152+
function download (url, params, fileName, loading,callback) {
153+
fileName = fileName.replace(">", ">").replace("<", "<");
154+
post(url, params, loading, { responseType: 'blob' }).then(content => {
155+
const blob = new Blob([content])
156+
if ('download' in document.createElement('a')) { // 非IE下载
157+
const elink = document.createElement('a')
158+
elink.download = fileName
159+
elink.style.display = 'none'
160+
elink.href = URL.createObjectURL(blob)
161+
document.body.appendChild(elink)
162+
elink.click()
163+
URL.revokeObjectURL(elink.href) // 释放URL 对象
164+
document.body.removeChild(elink)
165+
} else { // IE10+下载
166+
navigator.msSaveBlob(blob, fileName)
167+
}
168+
callback&&callback();
169+
})
170+
}
152171

153172

154173
function createXHR () {
@@ -293,4 +312,4 @@ ajax.post = function (url, param, success, errror) {
293312
ajax.get = function (url, param, success, errror) {
294313
ajax({ url: url, param: param, success: success, error: errror, type: 'get' })
295314
}
296-
export default { post, get, ajax, ipAddress }
315+
export default { post, get,download, ajax, ipAddress }

0 commit comments

Comments
 (0)