增加文件下载功能
This commit is contained in:
parent
c9316f53b0
commit
a6568d208b
|
|
@ -18,6 +18,7 @@
|
|||
"@vueuse/core": "^10.6.1",
|
||||
"axios": "^1.6.2",
|
||||
"element-plus": "^2.4.3",
|
||||
"file-saver": "^2.0.5",
|
||||
"js-base64": "^3.7.5",
|
||||
"mitt": "^3.0.1",
|
||||
"moment": "^2.29.4",
|
||||
|
|
|
|||
|
|
@ -20,6 +20,9 @@ importers:
|
|||
element-plus:
|
||||
specifier: ^2.4.3
|
||||
version: 2.4.3(vue@3.3.4)
|
||||
file-saver:
|
||||
specifier: ^2.0.5
|
||||
version: 2.0.5
|
||||
js-base64:
|
||||
specifier: ^3.7.5
|
||||
version: 3.7.5
|
||||
|
|
@ -809,6 +812,9 @@ packages:
|
|||
fastq@1.15.0:
|
||||
resolution: {integrity: sha512-wBrocU2LCXXa+lWBt8RoIRD89Fi8OdABODa/kEnyeyjS5aZO5/GNvI5sEINADqP/h8M29UHTHUb53sUu5Ihqdw==}
|
||||
|
||||
file-saver@2.0.5:
|
||||
resolution: {integrity: sha512-P9bmyZ3h/PRG+Nzga+rbdI4OEpNDzAVyy74uVO9ATgzLK6VtAsYybF/+TOCvrc0MO793d6+42lLyZTw7/ArVzA==}
|
||||
|
||||
filelist@1.0.4:
|
||||
resolution: {integrity: sha512-w1cEuf3S+DrLCQL7ET6kz+gmlJdbq9J7yXCSjK/OZCPA+qEN1WyF4ZAf0YYJa4/shHJra2t/d/r8SV4Ji+x+8Q==}
|
||||
|
||||
|
|
@ -2237,6 +2243,8 @@ snapshots:
|
|||
dependencies:
|
||||
reusify: 1.0.4
|
||||
|
||||
file-saver@2.0.5: {}
|
||||
|
||||
filelist@1.0.4:
|
||||
dependencies:
|
||||
minimatch: 5.1.6
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import axios from 'axios'
|
|||
import NProgress from 'nprogress'
|
||||
import { mainStore } from 'store/main'
|
||||
import { ElMessage } from "element-plus";
|
||||
import { saveAs } from 'file-saver'
|
||||
import router from "@/router"
|
||||
const store = mainStore()
|
||||
// const CancelToken = axios.CancelToken
|
||||
|
|
@ -123,24 +124,40 @@ export function upload(url: string, params: any) {
|
|||
})
|
||||
})
|
||||
}
|
||||
export function download(url: string, params: any) {
|
||||
export function download(url: string, params: any, fileName: string = 'downloaded_file') {
|
||||
return new Promise((resolve, reject) => {
|
||||
NProgress.start()
|
||||
NProgress.start(); // 开始进度条
|
||||
|
||||
service
|
||||
.post(url, params, {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
responseType: 'blob'
|
||||
responseType: 'blob' // 请求返回数据类型为 Blob(二进制流)
|
||||
})
|
||||
.then((res: any) => {
|
||||
resolve(res)
|
||||
NProgress.done(); // 结束进度条
|
||||
|
||||
// 判断返回的内容类型是否是 Blob
|
||||
const contentType = res.headers['content-type'] || '';
|
||||
if (!contentType.includes('application/octet-stream') && !contentType.includes('application/pdf')) {
|
||||
reject('文件类型不正确');
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理文件下载
|
||||
const blob = new Blob([res.data], { type: contentType });
|
||||
|
||||
// 使用 file-saver 保存文件
|
||||
saveAs(blob, fileName);
|
||||
|
||||
resolve('文件下载成功');
|
||||
})
|
||||
.catch((err) => {
|
||||
NProgress.done()
|
||||
reject(err.data)
|
||||
})
|
||||
})
|
||||
NProgress.done(); // 结束进度条
|
||||
reject(err?.data || '下载失败');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function put(url: string, params: any) {
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue