文件上传示例
本示例演示了如何使用 Go Fiber 处理文件上传。
描述
本项目提供了在 Go Fiber 应用中处理文件上传的基础设置。它包含上传单个和多个文件以及将文件保存到不同目录的示例。
要求
项目结构
single/main.go
: 上传单个文件到根目录的示例。single_relative_path/main.go
: 上传单个文件到相对路径的示例。multiple/main.go
: 上传多个文件的示例。go.mod
: Go 模块文件。
设置
-
克隆仓库
git clone https://github.com/gofiber/recipes.git
cd recipes/upload-file -
安装依赖
go mod download
运行示例
单文件上传
-
进入
single
目录cd single
-
运行应用
go run main.go
-
使用诸如
curl
或 Postman 的工具来上传文件curl -F "document=@/path/to/your/file" http://localhost:3000/
单文件上传到相对路径
-
进入
single_relative_path
目录cd single_relative_path
-
运行应用
go run main.go
-
使用诸如
curl
或 Postman 的工具来上传文件curl -F "document=@/path/to/your/file" http://localhost:3000/
多文件上传
-
进入
multiple
目录cd multiple
-
运行应用
go run main.go
-
使用诸如
curl
或 Postman 的工具来上传多个文件curl -F "documents=@/path/to/your/file1" -F "documents=@/path/to/your/file2" http://localhost:3000/
代码概览
single/main.go
处理单个文件上传到根目录。
single_relative_path/main.go
处理单个文件上传到相对路径。
multiple/main.go
处理多个文件上传。
结论
本示例提供了在 Go Fiber 应用中处理文件上传的基础设置。它可以进一步扩展和定制,以满足更复杂应用的需求。