fix: precheck local downloads to avoid JSON file download on quota errors

This commit is contained in:
2026-02-17 19:32:48 +08:00
parent dd6c439eb3
commit 2b700978ad
2 changed files with 91 additions and 10 deletions

View File

@@ -1562,7 +1562,7 @@ handleDragLeave(e) {
}
// 其他场景走后端下载接口(支持下载流量计量/权限控制)
this.downloadFromLocal(filePath);
await this.downloadFromLocal(filePath);
},
async downloadFromOSS(filePath) {
@@ -1595,15 +1595,35 @@ handleDragLeave(e) {
}
},
// 本地存储下载
downloadFromLocal(filePath) {
const url = `${this.apiBase}/api/files/download?path=${encodeURIComponent(filePath)}`;
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', '');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// 本地存储下载(先预检,避免浏览器下载 JSON 错误文件)
async downloadFromLocal(filePath) {
try {
const checkResp = await axios.get(`${this.apiBase}/api/files/download-check`, {
params: { path: filePath }
});
if (!checkResp.data?.success) {
this.showToast('error', '下载失败', checkResp.data?.message || '下载失败,请稍后重试');
return false;
}
const url = `${this.apiBase}/api/files/download?path=${encodeURIComponent(filePath)}`;
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', '');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
return true;
} catch (error) {
console.error('本地下载预检失败:', error);
const message = error.response?.data?.message || '下载失败,请稍后重试';
this.showToast('error', '下载失败', message);
if (error.response?.status === 401) {
this.logout();
}
return false;
}
},
// ===== 文件操作 =====