Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to binding for multi files upload #1878

Closed
wuhuizuo opened this issue May 2, 2019 · 7 comments · Fixed by #1949
Closed

how to binding for multi files upload #1878

wuhuizuo opened this issue May 2, 2019 · 7 comments · Fixed by #1949

Comments

@wuhuizuo
Copy link
Contributor

wuhuizuo commented May 2, 2019

#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

@yyq2013
Copy link

yyq2013 commented May 12, 2019

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.

@thinkerou
Copy link
Member

thinkerou commented Jun 7, 2019

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"`
}

@wuhuizuo
Copy link
Contributor Author

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 ?

@thinkerou
Copy link
Member

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

thinkerou pushed a commit that referenced this issue Jun 18, 2019
* binding: add support of multipart multi files (#1878)

* update readme: add multipart file binding
ThomasObenaus pushed a commit to ThomasObenaus/gin that referenced this issue Feb 19, 2020
…onic#1949)

* binding: add support of multipart multi files (gin-gonic#1878)

* update readme: add multipart file binding
@amirex128
Copy link

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

You must use something. Like that

@dsp1589
Copy link

dsp1589 commented Dec 5, 2022

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?

@kwamekyeimonies
Copy link

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"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants