How do I handle file uploads with Echo in Go?

Handling File Uploads with Echo in Go

File uploads are a common requirement in web applications. With Echo, a high-performance web framework for Go, handling file uploads is straightforward.

To get started, you need to install the Echo framework if you haven't done so already:

go get github.com/labstack/echo/v4

Here’s a simple example to handle file uploads:

package main import ( "net/http" "github.com/labstack/echo/v4" ) func main() { e := echo.New() e.POST("/upload", func(c echo.Context) error { // Retrieve the file from the request file, err := c.FormFile("file") if err != nil { return c.String(http.StatusBadRequest, "Failed to get the file") } // Save the file to the server if err := c.SaveFile(file, "uploads/" + file.Filename); err != nil { return c.String(http.StatusInternalServerError, "Failed to save the file") } return c.String(http.StatusOK, "File uploaded successfully") }) e.Start(":8080") }

Go Echo file uploads web framework server-side file handling Go web applications