Gin:文件上传

目录

1. 单文件上传

func uploadFile(c *gin.Context) {
	file, err := c.FormFile("file")
	if err == nil {
		dst := path.Join("./static", file.Filename)
		saveErr := c.SaveUploadedFile(file, dst)
		if saveErr == nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 0,
				"msg":  "success",
				"data": dst,
			})
		}
	}
}

2. 多文件上传

func uploadFiles(c *gin.Context) {
	var urls []string
	form, _ := c.MultipartForm()
	files := form.File["files"]
	for _, file := range files {
		dst := path.Join("./static", file.Filename)
		urls = append(urls, dst)
		c.SaveUploadedFile(file, dst)
	}
	fmt.Println(urls)
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": urls,
	})
}

3. 根据上传时间保存

func upload(c *gin.Context) {
	//1、获取上传的文件
	file, err1 := c.FormFile("file")
	if err1 == nil {
		//2、获取后缀名 判断类型是否正确 .jpg .png .gif .jpeg
		extName := path.Ext(file.Filename)
		allowExtMap := map[string]bool{
			".jpg":  true,
			".png":  true,
			".gif":  true,
			".jpeg": true,
		}
		if _, ok := allowExtMap[extName]; !ok {
			c.String(http.StatusBadRequest, "文件类型不合法")
			return
		}
		//3、创建图片保存目录,linux下需要设置权限(0666可读可写) static/upload/20200623
		day := time.Now().Format("20060102")
		dir := "./static/" + day
		if err := os.MkdirAll(dir, 0666); err != nil {
			c.String(http.StatusBadRequest, "MkdirAll失败")
			return
		}
		//4、生成文件名称 144325235235.png
		fileUnixName := strconv.FormatInt(time.Now().UnixNano(), 10)
		//5、上传文件 static/upload/20200623/144325235235.png
		saveDir := path.Join(dir, fileUnixName+extName)
		c.SaveUploadedFile(file, saveDir)
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "上传成功",
			"data": saveDir,
		})
	}
}

4. 配置路由

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"os"
	"path"
	"strconv"
	"time"
)

func uploadFile(c *gin.Context) {
	file, err := c.FormFile("file")
	if err == nil {
		dst := path.Join("./static", file.Filename)
		saveErr := c.SaveUploadedFile(file, dst)
		if saveErr == nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 0,
				"msg":  "success",
				"data": dst,
			})
		}
	}
}

func uploadFiles(c *gin.Context) {
	var urls []string
	form, _ := c.MultipartForm()
	files := form.File["files"]
	for _, file := range files {
		dst := path.Join("./static", file.Filename)
		urls = append(urls, dst)
		c.SaveUploadedFile(file, dst)
	}
	fmt.Println(urls)
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": urls,
	})
}

func upload(c *gin.Context) {
	//1、获取上传的文件
	file, err1 := c.FormFile("file")
	if err1 == nil {
		//2、获取后缀名 判断类型是否正确 .jpg .png .gif .jpeg
		extName := path.Ext(file.Filename)
		allowExtMap := map[string]bool{
			".jpg":  true,
			".png":  true,
			".gif":  true,
			".jpeg": true,
		}
		if _, ok := allowExtMap[extName]; !ok {
			c.String(http.StatusBadRequest, "文件类型不合法")
			return
		}
		//3、创建图片保存目录,linux下需要设置权限(0666可读可写) static/upload/20200623
		day := time.Now().Format("20060102")
		dir := "./static/" + day
		if err := os.MkdirAll(dir, 0666); err != nil {
			c.String(http.StatusBadRequest, "MkdirAll失败")
			return
		}
		//4、生成文件名称 144325235235.png
		fileUnixName := strconv.FormatInt(time.Now().UnixNano(), 10)
		//5、上传文件 static/upload/20200623/144325235235.png
		saveDir := path.Join(dir, fileUnixName+extName)
		c.SaveUploadedFile(file, saveDir)
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "上传成功",
			"data": saveDir,
		})
	}
}

func main() {

	engine := gin.Default()
	// 单文件上传
	engine.GET("/uploadFile", uploadFile)
	// 多文件上传
	engine.GET("/uploadFiles", uploadFiles)
	// 按上传时间保存文件
	engine.GET("/upload", upload)
	engine.Run()
}

5. 启动服务

  • 单文件上传,http://localhost:8080/uploadFile

Gin:文件上传
Gin:文件上传

  • 多文件上传,http://localhost:8080/uploadFiles

Gin:文件上传
Gin:文件上传

  • 根据上传时间保存,http://localhost:8080/upload

Gin:文件上传
Gin:文件上传

上一篇:轻量级HTTP文件共享服务


下一篇:TCPDUMP——抓包、筛选、高级筛选