dqyl2374 2018-09-25 13:19
浏览 93
已采纳

下载golang生成的文件时,文件下载可与Chrome一起使用,但不适用于Firefox

I have simple golang/gin-gonic REST Service that serves an excel report upon request to /api/billing. When the requester sets the accept header to application/vnd.openxmlformats-officedocument.spreadsheetml.sheet then an Excel file is served otherwise json. This code works fine in Chrome and IE but not with Firefox and I have no idea why.

In the FF debugger I see that the actual content is transmitted to the browser but FF does not offer the user the download dialog. So for the user it looks like as nothing is happening if he clicks on the link.

I checked already that the popups aren't blocked by FF also I disabled the other security features https://support.mozilla.org/1/firefox/62.0.2/Darwin/de/phishing-malware just in case. I also reinstalled plain FF without any extensions and any changes. The same happens on FF on windows.

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

Here are the request headers they are the same for FF & Chrome

HTTP Request:

    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

Response

    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
  • 写回答

2条回答 默认 最新

  • duanke8011 2018-09-28 13:32
    关注

    I tried to fix the issue for a couple of days. However I found the culprit where I didn't expect it. It finally was the behavior of the frontend application as I had the impression it was just a link that gets called and I was wrong.

    However I wanted to point out that I could successfully verify that the browser doesn't care if the Content-Disposition has a ', " or no quoting at all.

    Also the gin/golang above is working as it should.

    thx everyone for helping me out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?