mirror of
https://github.com/gin-gonic/gin.git
synced 2025-08-06 15:50:30 +08:00
add upload file example.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
39
examples/upload-file/multiple/main.go
Normal file
39
examples/upload-file/multiple/main.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
router.Static("/", "./public")
|
||||
router.POST("/upload", func(c *gin.Context) {
|
||||
name := c.PostForm("name")
|
||||
email := c.PostForm("email")
|
||||
|
||||
// Multipart form
|
||||
form, _ := c.MultipartForm()
|
||||
files := form.File["files"]
|
||||
|
||||
for _, file := range files {
|
||||
// Source
|
||||
src, _ := file.Open()
|
||||
defer src.Close()
|
||||
|
||||
// Destination
|
||||
dst, _ := os.Create(file.Filename)
|
||||
defer dst.Close()
|
||||
|
||||
// Copy
|
||||
io.Copy(dst, src)
|
||||
}
|
||||
|
||||
c.String(http.StatusOK, fmt.Sprintf("Uploaded successfully %d files with fields name=%s and email=%s.", len(files), name, email))
|
||||
})
|
||||
router.Run(":8080")
|
||||
}
|
Reference in New Issue
Block a user