Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d1f7f35

Browse files
committed
Add upload file example
1 parent 31e4401 commit d1f7f35

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,32 @@ func main() {
218218
id: 1234; page: 1; name: manu; message: this_is_great
219219
```
220220

221+
### Another example: upload file
222+
223+
Reference issue [#548](https://github.com/gin-gonic/gin/issues/548)
224+
225+
```go
226+
func main() {
227+
router := gin.Default()
228+
229+
router.POST("/upload", func(c *gin.Context) {
230+
231+
file, header , err := c.Request.FormFile("upload")
232+
filename := header.Filename
233+
fmt.Println(header.Filename)
234+
out, err := os.Create("./tmp/"+filename+".png")
235+
if err != nil {
236+
log.Fatal(err)
237+
}
238+
defer out.Close()
239+
_, err = io.Copy(out, file)
240+
if err != nil {
241+
log.Fatal(err)
242+
}
243+
})
244+
router.Run(":8080")
245+
}
246+
```
221247
222248
#### Grouping routes
223249
```go

0 commit comments

Comments
 (0)