下载链接:http://127.0.0.1:9999/test/down?filename=1.xlsx
创建一个main.go 直接贴代码
func main() {
r := gin.Default()
group := r.Group("/test")
//下载
group.GET("/down", func(c *gin.Context) {
//下载文件路径
downPath := "D:/down_file/"
//获取文件名
filename := c.Query("filename")
if len(filename) != 0 {
//拼接下载路径
filepath := downPath + filename
_, err := os.Stat(filepath)
if err != nil {
panic(err)
}
//文件的http头
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename="+filename)
c.Header("Content-Transfer-Encoding", "binary")
fmt.Println("下载路径:", filepath)
c.File(filepath)
return
}
})
r.Run(":9999")
}