Description
I'm trying to pass a file upload and a json with some data. I tried using both c.ShouldBindJSON() and c.FormFile("file"). They work individually but not together. I dont want to pass the json as form-data but I've tried that too but cant get it to find to the custom struct.
Basically the file I get will be saved and a link of it will be added to database along with some data passed through the same handler. I'm testing using postman.
How to reproduce
type Files struct {
Id int64
User_Id int64
FileName string
Type string
Name string
Link string
Created time.Time
Updated time.Time
}
func FileUploadHandler() func(c *gin.Context) {
return func(c *gin.Context) {
var model Files
err := c.ShouldBindJSON(&model)
file, err := c.FormFile("file")
if err != nil {
fmt.Println(err)
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
return
}
fileSrc := "../uploads/" + filename
// FILE SAVED
if err := c.SaveUploadedFile(file, fileSrc); err != nil {
c.JSON(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
return
}else{
err:=model.Add
if err!=nil{
c.JSON(http.StatusInternalServerError, fmt.Sprintf("upload file err: %s", err.Error()))
return
}else{
c.JSON(200, gin.H{
"success": "files added successfully",
})
}
}
}
}
Actual result
I get this error: multipart: NextPart: bufio: buffer full ```
Comment From: MohammadMobasher
You can use Bind instead of ShouldBindJSON
Comment From: Tezigudo
is this issue resolved yet?