问题
OK. I'm sure it does download XML files with the .xml extension, but I'm wondering what is missing in the code here to cause the .xml extenstion to be missing from the downloaded file.
Note: This works in IE 6+ (didn't try WebKit based browsers or 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();
}
Ideas anyone?
回答1:
Try changing:
response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
To:
response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
The code works for all browsers (including Firefox which we use heavily).
回答2:
Solved the firefox spaces problems. Surround your filename with quotes.
Change the code below
response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
to
response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
回答3:
Does your filename have space in it? Firefox may have problem with that.
See this blog post for more details:
http://blog.mjjames.co.uk/2009/04/content-disposition-in-different.html
来源:https://stackoverflow.com/questions/1120599/why-does-firefox-not-include-the-xml-extension-when-downloading-a-file