fix: 修复模型文件下载功能并改进错误处理
- 后端将 send_from_directory 替换为更可靠的 send_file - 前端添加 transformResponse 配置避免响应拦截器错误解析二进制数据 - 改进错误处理,添加详细日志和用户提示
This commit is contained in:
1 parent
4f25163b0e
commit
5be8973eae
2 files changed
+35
-16
No files matched your search
@@ -464,13 +464,18 @@ def download_model(model_id):
|
||||
if not os.path.exists(model.weight_path):
|
||||
return jsonify({"msg": "File not found"}), 404
|
||||
|
||||
model_dir = os.path.dirname(model.weight_path)
|
||||
return send_from_directory(
|
||||
model_dir,
|
||||
model.weight_filename,
|
||||
as_attachment=True,
|
||||
download_name=model.weight_filename,
|
||||
)
|
||||
try:
|
||||
# 使用 send_file 更可靠地发送文件
|
||||
from flask import send_file
|
||||
return send_file(
|
||||
model.weight_path,
|
||||
as_attachment=True,
|
||||
download_name=model.weight_filename,
|
||||
mimetype='application/octet-stream'
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to send file: {e}")
|
||||
return jsonify({"msg": "File send failed"}), 500
|
||||
|
||||
|
||||
@api.route("/admin/models/<int:model_id>/toggle", methods=["POST"])
|
||||
|
||||
@@ -253,17 +253,31 @@ const toggleStatus = async (row) => {
|
||||
|
||||
const download = async (row) => {
|
||||
try {
|
||||
const res = await api.get(`/admin/models/${row.id}/download`, { responseType: 'blob' })
|
||||
const url = window.URL.createObjectURL(res.data)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = row.weight_filename
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
a.remove()
|
||||
const res = await api.get(`/admin/models/${row.id}/download`, {
|
||||
responseType: 'blob',
|
||||
// 禁用响应拦截器的 JSON 解析
|
||||
transformResponse: [(data) => data]
|
||||
})
|
||||
|
||||
// 创建下载链接
|
||||
const blob = new Blob([res.data])
|
||||
const url = window.URL.createObjectURL(blob)
|
||||
const link = document.createElement('a')
|
||||
link.href = url
|
||||
link.download = row.weight_filename
|
||||
|
||||
// 触发下载
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
|
||||
// 清理
|
||||
document.body.removeChild(link)
|
||||
window.URL.revokeObjectURL(url)
|
||||
|
||||
ElMessage.success('下载已开始')
|
||||
} catch (error) {
|
||||
ElMessage.error('下载失败')
|
||||
console.error('Download error:', error)
|
||||
ElMessage.error('下载失败:' + (error.message || '未知错误'))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user