File Upload

To handle file upload in the Go Fiber framework, you can use the FormFile function of the context object. The function signature is as follows:

func (c *Ctx) FormFile(key string) (*multipart.FileHeader, error)

Here's an example of file upload:

app.Post("/", func(c *fiber.Ctx) error {
  // Read the file via the document key, and the file upload header must be in the multipart/form-data format
  file, err := c.FormFile("document")

  // Save the file to the specified directory
  return c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
})

File Download

To handle file download in Go Fiber, you can use the Download function as shown below:

app.Get("/", func(c *fiber.Ctx) error {
  return c.Download("./files/report-12345.pdf")
  // => Download report-12345.pdf

  return c.Download("./files/report-12345.pdf", "report.pdf")
  // => Download report.pdf
})