Basic file upload.

This commit is contained in:
CronyAkatsuki 2023-12-19 21:41:36 +01:00
parent b3c7dd7c41
commit b946ff7288

28
main.go
View File

@ -35,6 +35,8 @@ func main() {
e.GET("/", Index) e.GET("/", Index)
e.POST("/", Upload)
e.GET("/files/", Files) e.GET("/files/", Files)
e.Logger.Fatal(e.Start(":1323")) e.Logger.Fatal(e.Start(":1323"))
@ -58,3 +60,29 @@ func Index(c echo.Context) error {
func Files(c echo.Context) error { func Files(c echo.Context) error {
return c.Render(http.StatusOK, "files", "test") 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)
}