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