修正下载代码

This commit is contained in:
wangchengming 2025-12-11 22:18:44 +08:00
parent d3e218e597
commit 204eda7b04
2 changed files with 33 additions and 74 deletions

View File

@ -175,47 +175,33 @@ export default {
// const url = this.outPath + '\\' + this.queryParams.case_no + '_' + item + '_ROC.json'
// const path = url.replace(/\\/g, '/')
// this.$axios.get(window.CONFIG.baseUrl + '/download', { params: { path: path }})
// this.downloadFile(this.output_path, this.queryParams.case_no + '_' + item + '_ROC.json')
this.$axios.get(window.CONFIG.baseUrl + '/download', { params: { path: this.output_path }, responseType: 'blob', }).then(res => {
// 1.
if (!res.data) {
this.$message.error('下载失败:无文件数据');
return;
}
// 2.
let fileName = '下载文件'; //
const contentDisposition = res.headers['content-disposition'];
if (contentDisposition) {
// Content-Disposition
const fileNameMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
if (fileNameMatch && fileNameMatch[1]) {
fileName = decodeURIComponent(fileNameMatch[1].replace(/['"]/g, ''));
}
}
// 3.
const blob = new Blob([res.data]);
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
// 4.
link.href = downloadUrl;
link.download = fileName; //
link.style.display = 'none';
// 5.
document.body.appendChild(link);
link.click();
// 6.
setTimeout(() => {
document.body.removeChild(link);
window.URL.revokeObjectURL(downloadUrl); // URL
}, 100);
// this.downloadFile(this.output_path, this.queryParams.case_no + '_' + item + '_ROC.json')
this.$axios.get(window.CONFIG.baseUrl + '/download', { params: { path: this.output_path }, responseType: 'blob', }).then(res => {
const downLoadName = '下载文件_' + this.getCurrentTime() + '.xlsx'
// a
const a = document.createElement('a')
a.href = URL.createObjectURL(res)
// adownload
a.setAttribute('download', downLoadName)
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
})
})
},
getCurrentTime() {
//
var getTime = new Date().getTime(); //
var time = new Date(getTime); //
var year = time.getFullYear(); //
var month = (time.getMonth() + 1).toString().padStart(2, '0'); //
var date = time.getDate().toString().padStart(2, '0'); //
var hour = time.getHours().toString().padStart(2, '0'); //
var minute = time.getMinutes().toString().padStart(2, '0'); //
var second = time.getSeconds().toString().padStart(2, '0'); //
var gettime = year + month + date + hour + minute + second
return gettime
},
chart() {
this.myChart = this.$echarts.init(this.$refs.chartDom)
const option = {

View File

@ -177,42 +177,15 @@ export default {
// this.$axios.get(window.CONFIG.baseUrl + '/download', { params: { path: path }})
// this.downloadFile(path, this.queryParams.case_no + '_' + item + '_ROC.json')
this.$axios.get(window.CONFIG.baseUrl + '/download', { params: { path: this.output_path }, responseType: 'blob', }).then(res => {
// 1.
if (!res.data) {
this.$message.error('下载失败:无文件数据');
return;
}
// 2.
let fileName = '下载文件'; //
const contentDisposition = res.headers['content-disposition'];
if (contentDisposition) {
// Content-Disposition
const fileNameMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
if (fileNameMatch && fileNameMatch[1]) {
fileName = decodeURIComponent(fileNameMatch[1].replace(/['"]/g, ''));
}
}
// 3.
const blob = new Blob([res.data]);
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
// 4.
link.href = downloadUrl;
link.download = fileName; //
link.style.display = 'none';
// 5.
document.body.appendChild(link);
link.click();
// 6.
setTimeout(() => {
document.body.removeChild(link);
window.URL.revokeObjectURL(downloadUrl); // URL
}, 100);
const downLoadName = '下载文件_' + this.getCurrentTime() + '.xlsx'
// a
const a = document.createElement('a')
a.href = URL.createObjectURL(res)
// adownload
a.setAttribute('download', downLoadName)
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
})
})
},