Writing KML & KMZ files to the browser with ASP.Net

Over the last few weeks we have been busy finishing of various demos including our upcomming uk cinemas demo. During development we've come across a few 'issues' with writing KML an KMZ files to the users browser from an .aspx page using the "Content-Disposition" HTTP Header that I thought it would be helpful to share with other .Net Google Earth developers who may come acrross the same problems.

ALL the problems are due to Internet explorer! Firefox works perfectly all the time ;-) It seems I suffers from a number of problems, our specific ones being:

  1. IE often has problems honouring AddHeader("Content-Disposition","attachment; filename=myfile.kmz") and instead prompts the user to open a file called the same as the .aspx page they are on.
  2. IE also has problems actually downloading the file once you have the filename comming up ok, complaining about not being able to contact the internet site. Bizarely the page works fine it you follow a link to it from another page, but not if your enter the url directly into the address bar?
Both solutions in the end were simple, but not obvious
  1. If, just like us, you have IIS setup to compress all .aspx files (Gzip compression) then each .aspx request has a header "Content-Encoding: gzip" that IIS adds for you. Unfortunatly IE then ignores your "Content-Disposition" header, so to fix it add Response.AppendHeader("Content-Encoding", "none;") and then it works!
  2. This one is even simplier, but we have not idea why this works. Just add Response.ClearHeaders() and the file will work properly from a direct url
So other than now having a bigger hate for IE, its all working. We hope to have some of the first demos live soon.