【问题标题】:Why Does FireFox Not Include the .xml Extension when Downloading a File?为什么 FireFox 在下载文件时不包含 .xml 扩展名?
【发布时间】:2010-11-10 08:57:43
【问题描述】:

好的。我确定它确实会下载带有 .xml 扩展名的 XML 文件,但我想知道这里的代码中缺少什么导致下载的文件中缺少 .xml 扩展名。

注意:这适用于 IE 6+(未尝试基于 WebKit 的浏览器或 Opera)

    private void GenerateXmlAttachment(string xmlInStringFormat, string fileName)
    {
    // Where fileName = "someFile.xml"
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.Charset = string.Empty;
        response.ContentEncoding = Encoding.Default;

    response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        response.AddHeader("Content-Length", xmlInStringFormat.Length.ToString());
    response.ContentType = "text/xml";          

    response.Write(xmlInStringFormat);
        response.Flush();
        response.End();

    }

有什么想法吗?

【问题讨论】:

  • 你能绝对保证文件名有.xml吗?
  • 100% 绝对保证。刚刚再次检查,因为你让我怀疑它;)
  • 这是 Live HTTP 标头的响应标头:HTTP/1.x 200 OK 服务器:Microsoft-IIS/5.1 日期:2009 年 7 月 13 日星期一 17:05:14 GMT X-Powered-By : ASP.NET X-AspNet-Version: 2.0.50727 Content-Disposition: attachment;filename=test.xml Content-Length: 2040 Cache-Control: private Content-Type: text/xml

标签: asp.net firefox cross-browser http-headers mime-types


【解决方案1】:

您的文件名中有空格吗? Firefox 可能对此有问题。

有关详细信息,请参阅此博客文章:

http://blog.mjjames.co.uk/2009/04/content-disposition-in-different.html

【讨论】:

    【解决方案2】:

    尝试改变:

    response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);

    收件人:

    response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));

    该代码适用于所有浏览器(包括我们大量使用的 Firefox)。

    【讨论】:

    【解决方案3】:

    解决了火狐空间问题。 用引号将您的文件名括起来。

    更改下面的代码

    response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
    

    response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
    

    【讨论】:

    • 我更喜欢这个解决方案而不是公认的解决方案,因为它不会将空格转换为加号。
    猜你喜欢
    • 1970-01-01
    • 2011-04-07
    • 2011-04-07
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多