gin从reader读取数据

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

 

猜你喜欢

转载自www.cnblogs.com/chenguifeng/p/12203339.html