go gin的多文件(圖片)上傳

工具地址

https://github.com/lujiahaoo/gin-upload

先上圖

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

代碼

// Form is a parsed multipart form.
// Its File parts are stored either in memory or on disk,
// and are accessible via the *FileHeader's Open method.
// Its Value parts are stored as strings.
// Both are keyed by field name.
type Form struct {
	Value map[string][]string
	File  map[string][]*FileHeader
}

當你的Content-Type設置爲multipart/form-data,通過最上面的圖片我們打印MultpartForm可以看到傳輸過來的數據會被解析到上面代碼的結構體Form對應的map中,然後你就可以:

  1. 操作文件
func UploadImage(ctx *gin.Context) ([]string, error) {

//這個就是上面Form裏面的File map
fhs := ctx.Request.MultipartForm.File["image"]

for _, fheader := range fhs {
	saveUploadImage(fheader)
}
  1. 操作字段
//基本是一樣的,底層都是調用ParseMultipartForm解析
fmt.Println("r.FormValue:         ", ctx.PostForm("code"))
fmt.Println("r.FormValue:         ", ctx.Request.PostFormValue("code"))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章