Skip to main content

File Upload Example

Github StackBlitz

This example demonstrates how to handle file uploads using Go Fiber.

Description

This project provides a basic setup for handling file uploads in a Go Fiber application. It includes examples for uploading single and multiple files, as well as saving files to different directories.

Requirements

Project Structure

  • single/main.go: Example for uploading a single file to the root directory.
  • single_relative_path/main.go: Example for uploading a single file to a relative path.
  • multiple/main.go: Example for uploading multiple files.
  • go.mod: The Go module file.

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/upload-file
  2. Install the dependencies:

    go mod download

Running the Examples

Single File Upload

  1. Navigate to the single directory:

    cd single
  2. Run the application:

    go run main.go
  3. Use a tool like curl or Postman to upload a file:

    curl -F "document=@/path/to/your/file" http://localhost:3000/

Single File Upload with Relative Path

  1. Navigate to the single_relative_path directory:

    cd single_relative_path
  2. Run the application:

    go run main.go
  3. Use a tool like curl or Postman to upload a file:

    curl -F "document=@/path/to/your/file" http://localhost:3000/

Multiple File Upload

  1. Navigate to the multiple directory:

    cd multiple
  2. Run the application:

    go run main.go
  3. Use a tool like curl or Postman to upload multiple files:

    curl -F "documents=@/path/to/your/file1" -F "documents=@/path/to/your/file2" http://localhost:3000/

Code Overview

single/main.go

Handles uploading a single file to the root directory.

single_relative_path/main.go

Handles uploading a single file to a relative path.

multiple/main.go

Handles uploading multiple files.

Conclusion

This example provides a basic setup for handling file uploads in a Go Fiber application. It can be extended and customized further to fit the needs of more complex applications.

References