【发布时间】:2018-09-28 21:32:29
【问题描述】:
我有一个简单的 golang/gin-gonic REST 服务,可根据/api/billing 的请求提供一份 Excel 报告。当请求者将接受标头设置为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 时,将提供一个 Excel 文件,否则为 json。这段代码在 Chrome 和 IE 中运行良好,但在 Firefox 中无法运行,我不知道为什么。
在 FF 调试器中,我看到实际内容已传输到浏览器,但 FF 并未向用户提供下载对话框。因此,对于用户来说,如果他点击链接,就好像什么都没有发生。
我已经检查过弹出窗口没有被 FF 阻止,我还禁用了其他安全功能 https://support.mozilla.org/1/firefox/62.0.2/Darwin/de/phishing-malware 以防万一。我还重新安装了普通的 FF,没有任何扩展和任何更改。 Windows 上的 FF 也是如此。
r.GET("/api/billing", func(c *gin.Context) {
if c.GetHeader("Accept") == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" {
b := api.GetBillingReportExcel()
extraHeaders := map[string]string{
"Content-Disposition": "attachment;filename='BillingReport.xlsx'",
"Content-Transfer-Encoding": "binary",
"Content-Description": "Excel Billing Report",
}
c.DataFromReader(200, int64(b.Len()),"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",&b,extraHeaders)
}else {
billingTenants, _ := cache.Get(c.Request.RequestURI)
c.JSON(200, GetBillingData())
}
})
以下是 FF 和 Chrome 的请求标头
HTTP 请求:
Host: localhost:8081
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0
Accept: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://localhost:8081/
Connection: keep-alive
回应
HTTP/1.1 200 OK
X-Powered-By: Express
content-description: Excel Billing Report
content-disposition: attachment; filename='BillingReport.xlsx'
content-length: 11397
content-transfer-encoding: binary
content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
date: Tue, 25 Sep 2018 12:17:41 GMT
【问题讨论】:
-
RFC 建议 http 标头中的带引号的字符串应该用双引号 (
") 引用。我认为这就是问题所在。 -
而且根本不需要双引号,除非你的文件名中有空格。
标签: firefox go attachment go-gin