forked from mirror/gin
[ci skip] update readme for upload file.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
565e1e0c87
commit
6596aa3b71
60
README.md
60
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) {
|
router.POST("/upload", func(c *gin.Context) {
|
||||||
|
// single file
|
||||||
|
file, _ := c.FormFile("file")
|
||||||
|
log.Println(file.Filename)
|
||||||
|
|
||||||
file, header , err := c.Request.FormFile("upload")
|
c.String(http.StatusOK, "Uploaded...")
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
router.Run(":8080")
|
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) {
|
||||||
|
// Multipart form
|
||||||
|
form, _ := c.MultipartForm()
|
||||||
|
files := form.File["upload[]"]
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
log.Println(file.Filename)
|
||||||
|
}
|
||||||
|
c.String(http.StatusOK, "Uploaded...")
|
||||||
|
})
|
||||||
|
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()
|
||||||
|
|
Loading…
Reference in New Issue