platform-vue/docs/文件上传超时配置.md
2026-04-09 17:08:02 +08:00

127 lines
3.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 文件上传超时配置说明
## 修改内容
移除了文件上传的超时限制,并增加了文件大小限制,允许大文件(如软件安装包)上传完成而不会因超时或大小限制中断。
## 前端修改
### 修改位置
`platform/src/api/file.js` 中的 `uploadFile` 函数
### 修改详情
#### 修改前
```javascript
const config = {
url: "/platform/uploadfile",
method: "post",
data: formData,
timeout: 2 * 60 * 60 * 1000, // 2小时超时
};
```
#### 修改后
```javascript
const config = {
url: "/platform/uploadfile",
method: "post",
data: formData,
timeout: 0, // 不设置超时时间,等待文件上传完毕
};
```
## 后端修改
### 1. 文件大小限制调整
#### 修改位置
`go/controllers/platform_file.go`
#### 修改详情
##### 修改前
```go
const fileUploadMaxMB = 200 // 200MB
const fileUploadMaxBytes = fileUploadMaxMB * 1024 * 1024
```
##### 修改后
```go
const fileUploadMaxMB = 2048 // 2GB适用于大型软件安装包
const fileUploadMaxBytes = fileUploadMaxMB * 1024 * 1024
```
### 2. 服务器超时配置
#### 修改位置
`go/conf/app.conf`
#### 新增配置
```ini
# 服务器超时配置(支持大文件上传)
# 0 表示不设置超时限制
ServerTimeOut = 0
# 最大请求体大小字节0 表示不限制
MaxMemory = 0
```
## 影响范围
此修改影响所有使用 `uploadFile` 函数的文件上传功能,包括但不限于:
- 软件升级安装包上传 (`platform/src/views/platform/softwareupgrade/components/edit.vue`)
- 文件管理器上传 (`platform/src/views/system/fileManager/index.vue`)
- 其他调用 `uploadFile` API 的组件
## 技术说明
### 前端
- `timeout: 0` 在 axios 中表示不设置超时限制
- 上传进度仍然会正常显示(通过 `onUploadProgress` 回调)
- 用户可以随时取消上传操作
### 后端
- 文件大小限制从 200MB 提升到 2GB
- `ServerTimeOut = 0` 表示服务器不设置请求超时
- `MaxMemory = 0` 表示不限制请求体大小
- 适用于大文件(如软件安装包)的上传场景
## CORS 问题说明
如果遇到 CORS 错误,请确认:
1. 后端 `go/routers/router.go` 中已配置 CORS 中间件
2. 允许的请求头包含 `Authorization`
3. 生产环境中,需要将 `Access-Control-Allow-Origin` 设置为具体的前端域名
当前配置:
```go
ctx.Output.Header("Access-Control-Allow-Origin", "*")
ctx.Output.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS")
ctx.Output.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
```
## 注意事项
1. 修改后需要重启 Go 后端服务才能生效
2. 对于超大文件(>2GB建议考虑使用分片上传或断点续传机制
3. 生产环境建议配置 Nginx 等反向代理的超时和大小限制
4. 监控服务器磁盘空间,确保有足够空间存储大文件
## 相关文件
### 前端
- `platform/src/api/file.js` - 文件上传 API
- `platform/src/utils/request.js` - axios 请求配置
- `platform/src/views/platform/softwareupgrade/components/edit.vue` - 软件升级上传组件
### 后端
- `go/controllers/platform_file.go` - 文件上传控制器
- `go/conf/app.conf` - 服务器配置
- `go/routers/router.go` - CORS 配置
## 修改日期
2026-04-09