From b946ff7288b62d292b38a77d96ff19cc644734aa Mon Sep 17 00:00:00 2001 From: CronyAkatsuki Date: Tue, 19 Dec 2023 21:41:36 +0100 Subject: [PATCH] Basic file upload. --- main.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/main.go b/main.go index 068c291..7a408f9 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,8 @@ func main() { e.GET("/", Index) + e.POST("/", Upload) + e.GET("/files/", Files) e.Logger.Fatal(e.Start(":1323")) @@ -58,3 +60,29 @@ func Index(c echo.Context) error { func Files(c echo.Context) error { return c.Render(http.StatusOK, "files", "test") } + +func Upload(c echo.Context) error { + file, err := c.FormFile("file") + if err != nil { + return err + } + + src, err := file.Open() + if err != nil { + return err + } + + dst, err := os.Create("files/" + file.Filename) + if err != nil { + return err + } + defer dst.Close() + + if _, err = io.Copy(dst, src); err != nil { + return err + } + + fileUrl := c.Request().Host + "/files/" + file.Filename + "\n" + + return c.String(http.StatusOK, fileUrl) +}