Sfoglia il codice sorgente

[ci skip] update readme for upload file.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Bo-Yi Wu 9 anni fa
parent
commit
6596aa3b71
1 ha cambiato i file con 44 aggiunte e 14 eliminazioni
  1. 44 14
      README.md

+ 44 - 14
README.md

@@ -229,34 +229,64 @@ func main() {
 id: 1234; page: 1; name: manu; message: this_is_great
 id: 1234; page: 1; name: manu; message: this_is_great
 ```
 ```
 
 
-### Another example: upload file
+### upload file
 
 
-References issue [#548](https://github.com/gin-gonic/gin/issues/548).
+#### upload single file
+
+References issue [#774](https://github.com/gin-gonic/gin/issues/774).
 
 
 ```go
 ```go
 func main() {
 func main() {
 	router := gin.Default()
 	router := gin.Default()
+	router.POST("/upload", func(c *gin.Context) {
+		// single file
+		file, _ := c.FormFile("file")
+		log.Println(file.Filename)
+
+		c.String(http.StatusOK, "Uploaded...")
+	})
+	router.Run(":8080")
+}
+```
+
+curl command:
+
+```bash
+curl -X POST http://localhost:8080/upload \
+  -F "file=@/Users/appleboy/test.zip" \
+  -H "Content-Type: multipart/form-data"
+```
 
 
+#### upload multiple files
+
+```go
+func main() {
+	router := gin.Default()
 	router.POST("/upload", func(c *gin.Context) {
 	router.POST("/upload", func(c *gin.Context) {
+		// Multipart form
+		form, _ := c.MultipartForm()
+		files := form.File["upload[]"]
 
 
-	        file, header , err := c.Request.FormFile("upload")
-	        filename := header.Filename
-	        fmt.Println(header.Filename)
-	        out, err := os.Create("./tmp/"+filename+".png")
-	        if err != nil {
-	            log.Fatal(err)
-	        }
-	        defer out.Close()
-	        _, err = io.Copy(out, file)
-	        if err != nil {
-	            log.Fatal(err)
-	        }   
+		for _, file := range files {
+			log.Println(file.Filename)
+		}
+		c.String(http.StatusOK, "Uploaded...")
 	})
 	})
 	router.Run(":8080")
 	router.Run(":8080")
 }
 }
 ```
 ```
 
 
+curl command:
+
+```bash
+curl -X POST http://localhost:8080/upload \
+  -F "upload[]=@/Users/appleboy/test1.zip" \
+  -F "upload[]=@/Users/appleboy/test2.zip" \
+  -H "Content-Type: multipart/form-data"
+```
+
 #### Grouping routes
 #### Grouping routes
+
 ```go
 ```go
 func main() {
 func main() {
 	router := gin.Default()
 	router := gin.Default()