Why Does FireFox Not Include the .xml Extension when Downloading a File?

廉价感情. 提交于 2019-12-22 06:37:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!