记录下在gin中下载文件

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)
}

猜你喜欢

转载自blog.csdn.net/kuangshp128/article/details/127473951