单文件上传
我们使用multipart/form-data
格式上传文件,利用c.Request.FormFile
解析文件。
// HandleUploadFile 上传单个文件
func HandleUploadFile(c *gin.Context) {
file, header, err := c.Request.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": "文件上传失败"})
return
}
content, err := ioutil.ReadAll(file)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": "文件读取失败"})
return
}
fmt.Println(header.Filename)
fmt.Println(string(content))
c.JSON(http.StatusOK, gin.H{"msg": "上传成功"})
}
Reference
- https://razeencheng.com/post/gin-file-down-upload
- https://www.kancloud.cn/shuangdeyu/gin_book/949420
FEATURED TAGS
algorithm
algorithmproblem
architecturalpattern
architecture
aws
c#
cachesystem
codis
compile
concurrentcontrol
database
dataformat
datastructure
debug
design
designpattern
distributedsystem
django
docker
domain
engineering
freebsd
git
golang
grafana
hackintosh
hadoop
hardware
hexo
http
hugo
ios
iot
java
javaee
javascript
kafka
kubernetes
linux
linuxcommand
linuxio
lock
macos
markdown
microservices
mysql
nas
network
networkprogramming
nginx
node.js
npm
oop
openwrt
operatingsystem
padavan
performance
programming
prometheus
protobuf
python
redis
router
security
shell
software testing
spring
sql
systemdesign
truenas
ubuntu
vmware
vpn
windows
wmware
wordpress
xml
zookeeper