main.go 621 B

12345678910111213141516171819202122232425262728293031323334
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func main() {
  10. router := gin.Default()
  11. router.Static("/", "./public")
  12. router.POST("/upload", func(c *gin.Context) {
  13. name := c.PostForm("name")
  14. email := c.PostForm("email")
  15. // Source
  16. file, _ := c.FormFile("file")
  17. src, _ := file.Open()
  18. defer src.Close()
  19. // Destination
  20. dst, _ := os.Create(file.Filename)
  21. defer dst.Close()
  22. // Copy
  23. io.Copy(dst, src)
  24. c.String(http.StatusOK, fmt.Sprintf("File %s uploaded successfully with fields name=%s and email=%s.", file.Filename, name, email))
  25. })
  26. router.Run(":8080")
  27. }