1263's merged solution take no effect for multi files uploading.

my code:

// MailUpload struct for e-mail
type MailUpload struct {
    Mail string                  `json:"mail" form:"mail" binding:"required"`
    Attachments  []*multipart.FileHeader `json:"attachment,omitempty" form:"attachments" binding:"required"` // change form to "attachments[]" take no effect also.
}

func Mail(c *gin.Context) {
    var postData MailUpload
    err := c.Bind(&postData)
    if err != nil {
        fmt.Println(err)
        c.String(http.StatusBadRequest, err.Error())
    } else {
        fmt.Printf("%#v\n", postData)
        c.String(http.StatusOK, "ok")
    }
}

request send command by curl:

curl -v -H "Content-Type:multipart/form-data" -X POST http://localhost:8080/api/latest/newmail -F "attachments[]=@./2019-5-1_day_trends.png" -F mail=123

my error: Key: 'MailUpload.Attachments' Error:Field validation for 'Attachments' failed on the 'required' tag

Comment From: yyq2013

I have same issues, my form has two file fields, and one of it is file array, like following:

bindform

type UserProfileForm struct {
    OtherImages []*multipart.FileHeader `form:"images" binding:"-"`
    Avatar      *multipart.FileHeader `form:"avatar" binding:"-"`
    User        string                `form:"user" binding:"required"`
}

it only works as following:

bindForm

   type UserProfileForm struct {
    OtherImages *multipart.FileHeader `form:"images" binding:"-"`
    Avatar      *multipart.FileHeader `form:"avatar" binding:"-"`
    User        string                `form:"user" binding:"required"`
}

as a workaround, just get file array like this:

workround code

bindForm := UserProfileForm{}
if err := c.ShouldBind(&bindForm); err != nil {
    c.JSON(http.StatusBadRequest, gin.H{"message": "Bad form data found"})
    return
}
    mForm, _ := c.MultipartForm()
images := mForm.File["images"]
    bindForm.OtherImages = images

Wish gin can fix it.

Comment From: thinkerou

when we need to use []*multipart.FileHeader? I use postman, how upload multi file once? so, when you want to multi file, maybe you may use:

type Test struct {
  File1 *multipart.FileHeader `form: "file1" binding:"required"`
  File2 *multipart.FileHeader `form: "file2" binding:"required"`
}

Comment From: wuhuizuo

when we need to use []*multipart.FileHeader? I use postman, how upload multi file once? so, when you want to multi file, maybe you may use:

type Test struct { File1 *multipart.FileHeader `form: "file1" binding:"required"` File2 *multipart.FileHeader `form: "file2" binding:"required"` }

limit the file form key name is not a good idea。if you declare only file1 and file2, how to deal file3, file4 ?

Comment From: thinkerou

@wuhuizuo I known, []*multipart.FileHeader should also support. only curiosity what about scn.

Comment From: amirex128

type CreateProduct struct {
    Images           []*multipart.FileHeader `form:"images[]"`
}

You must use something. Like that

Comment From: dsp1589

I have same issues, my form has two file fields, and one of it is file array, like following:

bindform

type UserProfileForm struct { OtherImages []*multipart.FileHeader `form:"images" binding:"-"` Avatar *multipart.FileHeader `form:"avatar" binding:"-"` User string `form:"user" binding:"required"` }

it only works as following:

bindForm

type UserProfileForm struct { OtherImages *multipart.FileHeader `form:"images" binding:"-"` Avatar *multipart.FileHeader `form:"avatar" binding:"-"` User string `form:"user" binding:"required"` }

as a workaround, just get file array like this:

workround code

bindForm := UserProfileForm{} if err := c.ShouldBind(&bindForm); err != nil { c.JSON(http.StatusBadRequest, gin.H{"message": "Bad form data found"}) return } mForm, _ := c.MultipartForm() images := mForm.File["images"] bindForm.OtherImages = images

Wish gin can fix it.

Why it should be "-" for binding? cant we use "required" for files?

Comment From: kwamekyeimonies

how do I read in this case :type MasterTemplate struct { ID uuid.UUID form:"id" json:"id" IsVoice bool form:"is_voice" json:"is_voice" Title string form:"title" json:"title" IsSms bool form:"is_sms" json:"is_sms" FilePath string form:"file_path" json:"file_path" Content []struct { File *multipart.FileHeader form:"file" json:"file" MsgTmplateLangId string form:"msg_template_lang_id" json:"msg_template_lang_id" SMSContent string form:"sms_content" json:"sms_content" MsgContentID string form:"msg_content_id" json:"msg_content_id" } form:"content" json:"content" UserID uuid.UUID form:"user_id" json:"user_id" }