gofr icon indicating copy to clipboard operation
gofr copied to clipboard

How to Handle Multiple File Uploads in GoFr?

Open Thomas-Matheus opened this issue 2 months ago • 2 comments

Hi GoFr team! 👋

I'm using GoFr (v1.46.0) and currently have single file upload working perfectly. However, I'd like to extend this functionality to support multiple file uploads in a single request, and I'm not sure about the best approach with GoFr's context binding system.

Current Implementation (Single File Upload)

DTO Structure:

type FileUploadDTO struct {
    File *multipart.FileHeader `file:"file"`
}

Handler:

func (h *FileHandler) UploadFile(ctx *gofr.Context) (any, error) {
    var upload dto.FileUploadDTO
    if err := ctx.Bind(&upload); err != nil {
        return nil, err
    }
...
}

This works great for single files, but I need to support multiple files in one request.

What I'm Looking For

  1. Does GoFr's ctx.Bind() support binding multiple files automatically? For example, would something like this work:

    type MultiFileUploadDTO struct {
        Files []*multipart.FileHeader `file:"files"`
    }
    

Actually I tried this approach but didn't work properly

  1. Is there a recommended GoFr pattern for handling bulk file uploads with proper error handling and validation?

Use Case

Users need to upload multiple trip photos/documents simultaneously (e.g., 5-10 files at once) rather than making individual requests for each file.

Environment

  • GoFr Version: 1.46.0
  • Go Version: 1.25

Any guidance or examples would be greatly appreciated! Thanks for the awesome framework! 🚀

Thomas-Matheus avatar Oct 19 '25 21:10 Thomas-Matheus

Hi 👋,

Thanks for sharing this! Your single file upload implementation looks excellent.

Currently, ctx.Bind() doesn’t support binding multiple files *([]multipart.FileHeader) directly, which is why that approach didn’t work.

A simple and reliable way is to get the files from the request’s multipart form, like this:

func (h *FileHandler) UploadFiles(ctx *gofr.Context) (any, error) {
    if err := ctx.Request().ParseMultipartForm(32 << 20); err != nil { // 32MB max
        return nil, err
    }

    files := ctx.Request().MultipartForm.File["files"]
    if len(files) == 0 {
        return nil, fmt.Errorf("no files uploaded")
    }

    for _, fileHeader := range files {
        file, err := fileHeader.Open()
        if err != nil {
            return nil, err
        }
        defer file.Close()
        // Process each file here
    }

    return map[string]string{"status": "success"}, nil
}

This approach works well for multiple file uploads and gives full control for validation and error handling.

GoFr is already excellent, and this small adjustment makes multi-file uploads smooth! 🚀

WebCoreVision avatar Oct 22 '25 07:10 WebCoreVision

@WebCoreVision I will try this approach, thank very much for the answer. 😄

Thomas-Matheus avatar Oct 22 '25 14:10 Thomas-Matheus

Closing this issue, because of inactivity for a month. Please feel free to reopen if needed.

coolwednesday avatar Nov 19 '25 07:11 coolwednesday