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 關注
- 154 瀏覽
添加回答
舉報