2015-06-30 20:51:08 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2015-07-02 20:14:09 +02:00
|
|
|
mw "github.com/labstack/echo/middleware"
|
2015-06-30 20:51:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func upload(c *echo.Context) error {
|
|
|
|
req := c.Request()
|
2015-09-24 23:14:01 +02:00
|
|
|
req.ParseMultipartForm(16 << 20) // Max memory 16 MiB
|
2015-06-30 20:51:08 +02:00
|
|
|
|
|
|
|
// Read form fields
|
2015-07-05 20:08:17 +02:00
|
|
|
name := c.Form("name")
|
|
|
|
email := c.Form("email")
|
2015-06-30 20:51:08 +02:00
|
|
|
|
|
|
|
// Read files
|
|
|
|
files := req.MultipartForm.File["files"]
|
2015-07-02 06:22:51 +02:00
|
|
|
for _, f := range files {
|
2015-06-30 20:51:08 +02:00
|
|
|
// Source file
|
|
|
|
src, err := f.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
|
|
|
|
// Destination file
|
|
|
|
dst, err := os.Create(f.Filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
if _, err = io.Copy(dst, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.",
|
|
|
|
name, email, len(files))
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
e := echo.New()
|
2015-07-02 20:14:09 +02:00
|
|
|
e.Use(mw.Logger())
|
|
|
|
e.Use(mw.Recover())
|
|
|
|
|
|
|
|
e.Static("/", "public")
|
2015-06-30 20:51:08 +02:00
|
|
|
e.Post("/upload", upload)
|
2015-07-02 20:14:09 +02:00
|
|
|
|
2015-06-30 20:51:08 +02:00
|
|
|
e.Run(":1323")
|
|
|
|
}
|