How to Handle File Uploads in Gin

Web
Handle file uploads in Gin by parsing the multipart form and using FormFile to retrieve the uploaded file as an io.Reader.

Use c.Request.MultipartForm to parse the request body and access uploaded files via file, header, err := c.FormFile("name").

file, err := c.FormFile("file")
if err != nil {
    c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    return
}
src, err := file.Open()
if err != nil {
    c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    return
}
// Process src (io.Reader) here
src.Close()

Note: Ensure your form uses enctype="multipart/form-data" and set c.Request.MultipartForm if needed by calling c.Request.ParseMultipartForm(maxMemory) before accessing files.