1 回答
TA贡献1873条经验 获得超9个赞
您在浏览器接收到文件后添加标题,将它们移到之前io.Copy以使它们生效。此外,您在通话中有一个错误的格式动词(只是%),fmt.Sprintf请使用%q:
func DownloadObject(c *gin.Context) {
// Get rootPath & filePath from GET request
rootPath := c.Param("rootPath")
filePath := c.Param("filePath")
// ...
// Add headers to the response
c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filePath))
c.Writer.Header().Add("Content-Type", c.GetHeader("Content-Type"))
_, err = io.Copy(c.Writer, download)
if err != nil {
c.Status(http.StatusInternalServerError)
log.Print(err)
return
}
err = download.Close()
if err != nil {
c.Status(http.StatusInternalServerError)
log.Print(err)
return
}
}
- 1 回答
- 0 关注
- 127 浏览
添加回答
举报