Bladeren bron

add upload file example.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Bo-Yi Wu 9 jaren geleden
bovenliggende
commit
17af53a565

+ 3 - 1
README.md

@@ -233,7 +233,7 @@ id: 1234; page: 1; name: manu; message: this_is_great
 
 #### upload single file
 
-References issue [#774](https://github.com/gin-gonic/gin/issues/774).
+References issue [#774](https://github.com/gin-gonic/gin/issues/774) and detail [example code](examples/upload-file/single).
 
 ```go
 func main() {
@@ -259,6 +259,8 @@ curl -X POST http://localhost:8080/upload \
 
 #### upload multiple files
 
+See the detail [example code](examples/upload-file/multiple).
+
 ```go
 func main() {
 	router := gin.Default()

+ 39 - 0
examples/upload-file/multiple/main.go

@@ -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")
+}

+ 17 - 0
examples/upload-file/multiple/public/index.html

@@ -0,0 +1,17 @@
+<!doctype html>
+<html lang="en">
+<head>
+    <meta charset="utf-8">
+    <title>Multiple file upload</title>
+</head>
+<body>
+<h1>Upload multiple files with fields</h1>
+
+<form action="/upload" method="post" enctype="multipart/form-data">
+    Name: <input type="text" name="name"><br>
+    Email: <input type="email" name="email"><br>
+    Files: <input type="file" name="files" multiple><br><br>
+    <input type="submit" value="Submit">
+</form>
+</body>
+</html>

+ 34 - 0
examples/upload-file/single/main.go

@@ -0,0 +1,34 @@
+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")
+
+		// Source
+		file, _ := c.FormFile("file")
+		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("File %s uploaded successfully with fields name=%s and email=%s.", file.Filename, name, email))
+	})
+	router.Run(":8080")
+}

+ 16 - 0
examples/upload-file/single/public/index.html

@@ -0,0 +1,16 @@
+<!doctype html>
+<html lang="en">
+<head>
+    <meta charset="utf-8">
+    <title>Single file upload</title>
+</head>
+<body>
+<h1>Upload single file with fields</h1>
+
+<form action="/upload" method="post" enctype="multipart/form-data">
+    Name: <input type="text" name="name"><br>
+    Email: <input type="email" name="email"><br>
+    Files: <input type="file" name="file"><br><br>
+    <input type="submit" value="Submit">
+</form>
+</body>