func (i *ImgFileApi) DownloadFileApi(ctx *gin.Context) {
url := "xxxx"
response1, err := http.Get(url)
if err != nil || response1.StatusCode != http.StatusOK {
ctx.Status(http.StatusServiceUnavailable)
return
}
reader := response1.Body
contentLength := response1.ContentLength
contentType := response1.Header.Get("Content-Type")
fileName1 := "11.png"
extraHeaders := map[string]string{
"Content-Disposition": fmt.Sprintf(`attachment; filename="%s"`, fileName1),
}
ctx.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)
}
记录下在gin中下载文件
最新推荐文章于 2025-04-14 22:37:17 发布