Gin上传文件
Posted Harris-H
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gin上传文件相关的知识,希望对你有一定的参考价值。
文章目录
Gin上传文件
1.单文件上传
项目结构
main.go
package main
import (
"fmt"
"net/http"
"path/filepath"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.Static("/", "./upload-file/single/public") //访问静态文件
router.POST("/upload", func(c *gin.Context) {
name := c.PostForm("name")
email := c.PostForm("email")
// Source
file, err := c.FormFile("file")
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
return
}
filename := filepath.Base(file.Filename) //返回路径的最后一个元素
pre := "./upload-file/single/images/"
if err := c.SaveUploadedFile(file, pre+filename); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
return
}
c.String(http.StatusOK, fmt.Sprintf("File %s uploaded successfully with fields name=%s and email=%s.", file.Filename, name, email))
})
router.Run(":8080")
}
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Single file upload</title>
</head>
<body>
<h1>Upload single file with fields</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
Files: <input type="file" name="file"><br><br>
<input type="submit" value="Submit">
</form>
</body>
说明
a.Engine的MaxMultipartMemory
// Value of 'maxMemory' param that is given to http.Request's ParseMultipartForm
// method call.
MaxMultipartMemory int64
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB
类型是int64
, 8<<20
就是
8
B
×
2
20
=
8
M
B
8B\\times 2^{20}=8MB
8B×220=8MB
b.filepath.Base(path)
返回路径的最后一个元素。
c. PostForm(key)
返回post提交的form表单对应键的值。
d. FormFile(name)
返回form表达对应名称的文件。
结果
2.多文件上传
main.go
package main
import (
"fmt"
"net/http"
"path/filepath"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.Static("/", "./upload-file/multiple/public")
router.POST("/upload", func(c *gin.Context) {
name := c.PostForm("name")
email := c.PostForm("email")
// Multipart form
form, err := c.MultipartForm()
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
return
}
files := form.File["files"] //找到对应的name的input
pre := "./upload-file/multiple/images/"
for _, file := range files { //遍历文件
filename := filepath.Base(file.Filename)
if err := c.SaveUploadedFile(file, pre+filename); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
return
}
}
c.String(http.StatusOK, fmt.Sprintf("Uploaded successfully %d files with fields name=%s and email=%s.", len(files), name, email))
})
router.Run(":8081")
}
结果
说明
index.html
唯一的区别就是在input 里面加上multiple
,这样就支持多文件了。
MultipartForm()
方法返回一个form结构体。
type Form struct {
Value map[string][]string
File map[string][]*FileHeader
}
包含两个属性,一个存放文件,一个存放value。
以上是关于Gin上传文件的主要内容,如果未能解决你的问题,请参考以下文章