Golang中如何读取数组结构体中的multipart.Fileheader
Golang中如何读取数组结构体中的multipart.Fileheader
type MasterTemplate struct {
ID string `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"`
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"`
FilePath string `form:"file_path" json:"file_path"`
} `form:"content" json:"content"`
UserID uuid.UUID `form:"user_id" json:"user_id"`
}
func (tmplController *MessageTemplateController) CreateMsgTemplateController(ctx *gin.Context) {
// var msgTemplate models.MasterTemplate
err := ctx.Request.ParseMultipartForm(10 * 1024 * 1024)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
var contents []models.MasterTemplate
form := ctx.Request.MultipartForm
files := form.File["content[file]"]
// formFileds := form.Value
// formFields := ctx.Request.MultipartForm.Value
for x, file := range files {
var msgTemplate models.MasterTemplate
msgTemplate.Content = make([]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"`
FilePath string `form:"file_path" json:"file_path"`
}, len(files))
// Bind the form data to the struct
// Set the file data for this content item
msgTemplate.Content[x].File = file
contents = append(contents, msgTemplate)
}
ctx.JSON(http.StatusOK, gin.H{
"response": contents,
})
}
我如何获取文件及其关联信息。附件是我的 Postman 请求截图:

更多关于Golang中如何读取数组结构体中的multipart.Fileheader的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复


