这是一个演示如何使用 Tenon.AspNetCore 中的文件上传功能的示例项目。该示例展示了如何配置和使用文件上传验证、本地化支持以及外部资源文件上传功能。
-
文件上传验证
- 文件大小限制
- 文件类型限制
- 文件名验证
- 外部资源文件支持
-
本地化支持
- 支持中文和英文
- 可扩展支持其他语言
- 错误消息本地化
-
API 接口
- 单文件上传
- 多文件上传
- 外部资源文件上传
在 appsettings.json 中配置文件上传选项:
{
"FileUploadSettings": {
"MaxRequestBodySize": 104857600, // 最大请求体大小(100MB)
"MaxFileSize": 10485760, // 单个文件最大大小(10MB)
"AllowedExtensions": [ // 允许的文件扩展名
".jpg",
".png",
".pdf"
],
"ValidateFileName": true, // 是否验证文件名
"AllowExternalResources": true, // 是否允许外部资源文件
"ExternalResourceTimeout": "00:00:30", // 外部资源下载超时时间
"AllowedExternalDomains": [ // 允许的外部资源域名
"example.com",
"images.example.com"
]
}
}本地化资源文件位于 Resources 目录:
FileValidation.en.resx:英文资源文件FileValidation.zh.resx:中文资源文件
要添加新的语言支持,只需要:
- 在
Resources目录添加对应的资源文件(如FileValidation.fr.resx) - 在
Program.cs中的supportedCultures数组添加新的语言文化信息
POST /api/file/upload
Content-Type: multipart/form-data
file: (binary)POST /api/file/upload-multiple
Content-Type: multipart/form-data
files: (binary)POST /api/file/upload-external
Content-Type: multipart/form-data
url: https://example.com/image.jpg要测试不同语言的错误消息,可以在请求头中设置 Accept-Language:
// 英文
Accept-Language: en
// 中文
Accept-Language: zh- 确保已安装 .NET 9.0 SDK
- 克隆仓库
- 进入项目目录:
cd samples/FileUploadSample - 运行项目:
dotnet run - 访问 Swagger UI:
https://localhost:5001/swagger
- 上传的文件保存在项目根目录的
uploads文件夹中 - 文件名会自动生成为 GUID,以避免文件名冲突
- 外部资源文件上传时会自动下载并保存为本地文件
- 请确保配置文件中的文件大小限制符合你的需求
- 生产环境中建议配置更安全的文件存储位置