Gin:文件上传

Gin:文件上传,第1张

目录 1. 单文件上传2. 多文件上传3. 根据上传时间保存4. 配置路由5. 启动服务

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


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


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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/995868.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存