Serve easily dynamic files with DataFromReader context method (#1304)

* Add DataFromReader context method

* Replace fmt by strconv.FormatInt

* Add pull request link to README
This commit is contained in:
Jean-Christophe Lebreton
2018-05-12 05:00:42 +02:00
committed by Bo-Yi Wu
parent 5636afe02d
commit bf7803815b
6 changed files with 116 additions and 0 deletions

View File

@ -40,6 +40,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
- [XML, JSON and YAML rendering](#xml-json-and-yaml-rendering)
- [JSONP rendering](#jsonp)
- [Serving static files](#serving-static-files)
- [Serving data from reader](#serving-data-from-reader)
- [HTML rendering](#html-rendering)
- [Multitemplate](#multitemplate)
- [Redirects](#redirects)
@ -901,6 +902,32 @@ func main() {
}
```
### Serving data from reader
```go
func main() {
router := gin.Default()
router.GET("/someDataFromReader", func(c *gin.Context) {
response, err := http.Get("https://raw.githubusercontent.com/gin-gonic/logo/master/color.png")
if err != nil || response.StatusCode != http.StatusOK {
c.Status(http.StatusServiceUnavailable)
return
}
reader := response.Body
contentLength := response.ContentLength
contentType := response.Header.Get("Content-Type")
extraHeaders := map[string]string{
"Content-Disposition": `attachment; filename="gopher.png"`,
}
c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)
})
router.Run(":8080")
}
```
### HTML rendering
Using LoadHTMLGlob() or LoadHTMLFiles()