GO语言gin框架实战-05-文件下载

2022-11-14  本文已影响0人  玄德公笔记

语法

func (c *Context) File(filepath string)
    c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
    c.Writer.Header().Add("Content-Type", "application/octet-stream")

    c.File(filePath)

完整示例

main同级目录下创建down目录,目录下创建test.log,随便写一些内容进去。

func DownFile(c *gin.Context)  {

    fileName := test.log
    filePath := "down/"+fileName


    c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", test.log))
    c.Writer.Header().Add("Content-Type", "application/octet-stream")

    fmt.Println(filePath)
    c.File(filePath)
}
func ServerWebsocket()  {
    r := gin.Default()
    ……
    deployment := r.Group("/api/v1/logger")
    {
        ……
        deployment.GET("/file",DownLog)


    }
    r.Run(":1911")
}

postman下载测试

image.png
上一篇 下一篇

猜你喜欢

热点阅读