文件上传

上传单个文件

服务端

server.go

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "net/http"
  7. "github.com/labstack/echo"
  8. "github.com/labstack/echo/middleware"
  9. )
  10. func upload(c echo.Context) error {
  11. // Read form fields
  12. name := c.FormValue("name")
  13. email := c.FormValue("email")
  14. //-----------
  15. // Read file
  16. //-----------
  17. // Source
  18. file, err := c.FormFile("file")
  19. if err != nil {
  20. return err
  21. }
  22. src, err := file.Open()
  23. if err != nil {
  24. return err
  25. }
  26. defer src.Close()
  27. // Destination
  28. dst, err := os.Create(file.Filename)
  29. if err != nil {
  30. return err
  31. }
  32. defer dst.Close()
  33. // Copy
  34. if _, err = io.Copy(dst, src); err != nil {
  35. return err
  36. }
  37. return c.HTML(http.StatusOK, fmt.Sprintf("<p>File %s uploaded successfully with fields name=%s and email=%s.</p>", file.Filename, name, email))
  38. }
  39. func main() {
  40. e := echo.New()
  41. e.Use(middleware.Logger())
  42. e.Use(middleware.Recover())
  43. e.Static("/", "public")
  44. e.POST("/upload", upload)
  45. e.Logger.Fatal(e.Start(":1323"))
  46. }

客户端

index.html

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Single file upload</title>
  6. </head>
  7. <body>
  8. <h1>Upload single file with fields</h1>
  9. <form action="/upload" method="post" enctype="multipart/form-data">
  10. Name: <input type="text" name="name"><br>
  11. Email: <input type="email" name="email"><br>
  12. Files: <input type="file" name="file"><br><br>
  13. <input type="submit" value="Submit">
  14. </form>
  15. </body>
  16. </html>

上传多个文件

服务端

server.go

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "net/http"
  7. "github.com/labstack/echo"
  8. "github.com/labstack/echo/middleware"
  9. )
  10. func upload(c echo.Context) error {
  11. // Read form fields
  12. name := c.FormValue("name")
  13. email := c.FormValue("email")
  14. //------------
  15. // Read files
  16. //------------
  17. // Multipart form
  18. form, err := c.MultipartForm()
  19. if err != nil {
  20. return err
  21. }
  22. files := form.File["files"]
  23. for _, file := range files {
  24. // Source
  25. src, err := file.Open()
  26. if err != nil {
  27. return err
  28. }
  29. defer src.Close()
  30. // Destination
  31. dst, err := os.Create(file.Filename)
  32. if err != nil {
  33. return err
  34. }
  35. defer dst.Close()
  36. // Copy
  37. if _, err = io.Copy(dst, src); err != nil {
  38. return err
  39. }
  40. }
  41. return c.HTML(http.StatusOK, fmt.Sprintf("<p>Uploaded successfully %d files with fields name=%s and email=%s.</p>", len(files), name, email))
  42. }
  43. func main() {
  44. e := echo.New()
  45. e.Use(middleware.Logger())
  46. e.Use(middleware.Recover())
  47. e.Static("/", "public")
  48. e.POST("/upload", upload)
  49. e.Logger.Fatal(e.Start(":1323"))
  50. }

客户端

index.html

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Multiple file upload</title>
  6. </head>
  7. <body>
  8. <h1>Upload multiple files with fields</h1>
  9. <form action="/upload" method="post" enctype="multipart/form-data">
  10. Name: <input type="text" name="name"><br>
  11. Email: <input type="email" name="email"><br>
  12. Files: <input type="file" name="files" multiple><br><br>
  13. <input type="submit" value="Submit">
  14. </form>
  15. </body>
  16. </html>