asp.net - 为什么 FireFox 下载文件时不包含 .xml 扩展名?

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

好的。我确信它确实下载了带有 .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();

    }

有人有想法吗?

最佳答案

尝试改变:

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

致:

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

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

关于asp.net - 为什么 FireFox 下载文件时不包含 .xml 扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1120599/

相关文章:

c# - 将文件从一台服务器复制到另一台服务器时设备未准备好

asp.net - asp.net Web API 与发出 JSON 的 asp.net MVC Controller 的性能?

javascript - 代码使 Firefox 崩溃

javascript - Jquery SVG : How to get callback if browsers don't support svg?

php - 获取表单数据并将其发送到浏览器中的新选项卡

html - 负 z-index 有副作用吗

c# - 如何使用 LINQ - Entity Framework 从表中选择存在于联结表中的元素

c# - 有没有办法显示实现接口(interface)方法的智能感知注释?

html - Chrome 检查工具不反射(reflect)浏览器窗口,对齐 Div

javascript - $.ajax() 没有在 mozilla firefox 中向服务器端触发请求