• 从 reader 读取数据

    从 reader 读取数据

    1. func main() {
    2. router := gin.Default()
    3. router.GET("/someDataFromReader", func(c *gin.Context) {
    4. response, err := http.Get("https://raw.githubusercontent.com/gin-gonic/logo/master/color.png")
    5. if err != nil || response.StatusCode != http.StatusOK {
    6. c.Status(http.StatusServiceUnavailable)
    7. return
    8. }
    9. reader := response.Body
    10. contentLength := response.ContentLength
    11. contentType := response.Header.Get("Content-Type")
    12. extraHeaders := map[string]string{
    13. "Content-Disposition": `attachment; filename="gopher.png"`,
    14. }
    15. c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)
    16. })
    17. router.Run(":8080")
    18. }