一、问题描述
ginexcel
response.xls
filename := "测试.xlsx"response.xls
二、解决
Content-Disposition
如果,你设置成了下面这样,那么就会触发中文文件名乱码问题:(注意这是错误的设置!)
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
解决这个问题,需要修改两个地方:
Content-Disposition
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename*=utf-8''%s", filename))
*=utf-8''
filenameurl.QueryEscape
filename := url.QueryEscape("测试.xlsx")
部分关键代码示例:
filename := url.QueryEscape("测试.xlsx")
c.Writer.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
c.Writer.Header().Set("Pragma", "no-cache")
c.Writer.Header().Set("Expires", "0")
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename*=utf-8''%s", filename))
c.Writer.Header().Set("Content-Type", "application/vnd.ms-excel")
c.Writer.Header().Set("Content-Transfer-Encoding", "binary")
http.ServeContent(c.Writer, c.Request, filename, time.Now(), cont)
utf-8
在弹出窗口中,看到的是正确的文件名:
【END】