修正下载代码
This commit is contained in:
parent
d3e218e597
commit
204eda7b04
|
|
@ -177,45 +177,31 @@ export default {
|
|||
// 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);
|
||||
const downLoadName = '下载文件_' + this.getCurrentTime() + '.xlsx'
|
||||
// 通过a标签打开新页面下载文件
|
||||
const a = document.createElement('a')
|
||||
a.href = URL.createObjectURL(res)
|
||||
// a标签里有download属性可以自定义文件名
|
||||
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 = {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
// a标签里有download属性可以自定义文件名
|
||||
a.setAttribute('download', downLoadName)
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
document.body.removeChild(a)
|
||||
})
|
||||
})
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user