【问题标题】:How to name a file for download on Firefox?如何在 Firefox 上命名要下载的文件?
【发布时间】:2010-11-12 08:19:36
【问题描述】:

以下标题在 IE 上有效,但在 FF 上无效

<%@ Page Language="C#" ContentType="text/csv" Inherits="System.Web.Mvc.ViewPage"%>
<% Response.AddHeader("Content-Disposition", "filename=report.csv;attachment"); %>

在 FF 中,建议的名称在 FF 中显示为“report”,不带扩展名。

【问题讨论】:

    标签: asp.net asp.net-mvc http firefox attachment


    【解决方案1】:

    最近我也遇到了这个问题,终于找到解决办法了

    要使其在 Firefox 中正常工作,请确保在文件名中正确放置引号并且文件名中没有空格

    所以here是做基本测试的样例

    这会起作用:

    Response.ClearContent();
    Response.ContentType = "text/csv";
    Response.AddHeader("Content-Disposition", "attachment;filename="+ "MyOrders"+ "_Date_"
    +DateTime.Now.ToString("d")+".csv");
    

    这也将起作用

    string myfilename = "MyOrders" + "_Date_" + DateTime.Now.ToString("d") + ".csv";
    Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}", 
    myfilename));
    
    //here you can check using the break point,weather you are using any extra quotes in filename
    

    这在 Firefox 中不起作用

    Response.AddHeader("Content-Disposition", "attachment;filename="+ "MyOrders"+ "_Date_"
    +DateTime.Now+".csv");
    
    // because in DateTime.Now there is spaces in Time, Seconds , so firefox downloads file not 
    //as csv but downloads it as File , may be a binary file or unknown file type
    

    使用的引用 thisthisthisthis

    感谢所有以某种方式提供帮助的人。

    希望它可以帮助某人。

    【讨论】:

      【解决方案2】:

      try "attachment; filename=\"report.csv\""(即带引号的文件名)

      【讨论】:

        【解决方案3】:

        我目前正在使用如下代码:

        response.AddHeader("Content-Disposition", "attachment; filename=\"data.csv\"");
        

        在我的日常工作中——效果很好。您还确定这不是您的操作系统或任何启用了“隐藏已知文件类型的扩展名”选项的东西吗? (我知道 Windows 有这个选项,这让我抓狂)

        【讨论】:

        • 可能是因为他链接了文档
        • 可能,idk...我认为解决方案是使用引号(被转义)但老实说我不知道​​ - 我发布了这个,因为我知道它有效对我来说,我没有链接到文档或任何东西,因为它是我几个月前创建的代码,并且从那时起一直使用没有任何问题。
        【解决方案4】:

        当我需要实现 CSV 生成和下载时,这个关于 CSV 生成的问题帮助了我:How do I best generate a CSV (comma-delimited text file) for download with ASP.NET?

        【讨论】:

        • 感谢您的帖子,它帮助解决了 firefox 的问题here 是解决方案
        【解决方案5】:

        filename 只是Content-Dispostion 的一个参数。所以你必须交换两者:

        <% Response.AddHeader("Content-Disposition", "attachment;filename=report.csv"); %>
        

        【讨论】:

          猜你喜欢
          • 2011-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-22
          • 1970-01-01
          • 1970-01-01
          • 2015-03-04
          • 2018-10-17
          相关资源
          最近更新 更多