views:

371

answers:

3

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?

A: 

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

SolutionYogi
+1  A: 

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).

@Jose. Yup that's it. I found it about 30 minutes ago on this post, http://www.webmaster-talk.com/asp-forum/35962-content-disposition-does-nto-work-firefox.html. Thanks for posting.
nickyt
A: 

what about if the file name has space in it?

hajie