文章目录
- Gin文件上传
-
- 一 上传文件
-
- 1.1 上传单个文件
- 1.2 上传多个文件之不同名
- 1.3 上传多个文件之同名
- 1.4 上传多个图片-按照归档存储
-
Gin文件上传
一 上传文件
1.1 上传单个文件
- multipart/form-data格式用于文件上传
- gin文件上传与原生的net/http方法类似,不同在于gin把原生的request封装到c.Request中
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.MaxMultipartMemory = 8 << 20
r.POST("/upload", func(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
c.String(500, "上传图片出错")
}
dst := path.Join("./media/upload", file.Filename)
fmt.Println(dst)
c.SaveUploadedFile(file,dst)
c.String(http.StatusOK, file.Filename)
})
r.Run()
}
1.2 上传多个文件之不同名
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"path"
)
func main() {
router := gin.Default()
router.MaxMultipartMemory = 8 << 20
router.POST("/upload", func(c *gin.Context) {
file1, _ := c.FormFile("file1")
file2, _ := c.FormFile("file2")
dst1 := path.Join("./media/upload", file1.Filename)
c.SaveUploadedFile(file1,dst1)
dst2 := path.Join("./media/upload", file2.Filename)
c.SaveUploadedFile(file2,dst2)
c.String(http.StatusOK, "文件上传成功")
})
router.Run(":8000")
}
1.3 上传多个文件之同名
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"path"
)
func main() {
router := gin.Default()
router.MaxMultipartMemory = 8 << 20
router.POST("/upload", func(c *gin.Context) {
form, err := c.MultipartForm()
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("get err %s", err.Error()))
}
files := form.File["files"]
for _, file := range files {
dst := path.Join("./media/upload", file.Filename)
if err := c.SaveUploadedFile(file, dst); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("上传失败 %s", err.Error()))
return
}
}
c.String(200, fmt.Sprintf("上传 %d 个文件", len(files)))
})
router.Run(":8000")
}
1.4 上传多个图片-按照归档存储
package main
import (
"fmt"
"gin_test/utils"
"github.com/gin-gonic/gin"
"log"
"os"
"path"
"strconv"
)
func main() {
router := gin.Default()
router.MaxMultipartMemory = 8 << 20
router.POST("/upload", func(c *gin.Context) {
file, _ := c.FormFile("file")
extName := path.Ext(file.Filename)
allowExtMap := map[string]bool{
".jpg": true,
".png": true,
".gif": true,
".jpeg": true,
".md": true,
}
if _, ok := allowExtMap[extName]; !ok {
c.String(200, "文件类型不合法")
return
}
day := utils.GetDay()
dir := "./media/" + day
if err := os.MkdirAll(dir, 0777); err != nil {
log.Println(err)
}
fileUnixName := strconv.FormatInt(utils.GetUnix(), 10)
saveDir := path.Join(dir, fileUnixName+extName)
fmt.Println(saveDir)
c.SaveUploadedFile(file, saveDir)
c.String(200, "上传文件成功")
})
router.Run(":8000")
}
utils/common.go
package utils
import (
"crypto/md5"
"fmt"
"time"
)
func UnixToDate(timestamp int) string {
t := time.Unix(int64(timestamp), 0)
return t.Format("2006-01-02 15:04:05")
}
func DateToUnix(str string) int64 {
template := "2006-01-02 15:04:05"
t, err := time.ParseInLocation(template, str, time.Local)
if err != nil {
return 0
}
return t.Unix()
}
func GetUnix() int64 {
return time.Now().Unix()
}
func GetDate() string {
template := "2006-01-02 15:04:05"
return time.Now().Format(template)
}
func GetDay() string {
template := "20060102"
return time.Now().Format(template)
}
func Md5(str string) string {
data := []byte(str)
return fmt.Sprintf("%x\n", md5.Sum(data))
}