The following code will not run correctly in IE7 with the latest service packs installed.
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.Clear(); response.AddHeader('Content-Disposition', 'attachment;filename=Contacts.xls'); response.ContentType = 'application/octet-stream'; System.Text.UnicodeEncoding Encoding = new System.Text.UnicodeEncoding(); byte[] unicodeBytes = {255,254}; int length = 2 + Encoding.GetByteCount(_exportContent); // _exportContent is string. response.AddHeader('Content-Length', length.ToString()); response.OutputStream.Write(unicodeBytes, 0, 2); unicodeBytes = Encoding.GetBytes(_exportContent); response.OutputStream.Write(unicodeBytes, 2, unicodeBytes.Length); response.End();
I am opening the aspx page with js (window.open()) and execute the above code in the Page_Load().
The strange thing is that the window pops up, tries to load/show the file dialog and then you hear the sound like a popup window has been blocked (although popup blocker is deactivated!).
Extra information:
– The behavior happens both on XP and W2k3 (which is a real web server without anything else installed but IE7 & FW 3.5 SP1 & latest service packs.) – The same code works fine with FW 2.0 – Firefox has no problems to display a file dialog.
I would be curious if anyone else has ran into the same problem and could provide a solution for getting the thing working in IE7.
Cheers,
Dimi
Add a Header telling IE explicitly to CACHE the file. IE has known bugs with not being able to properly save a file if it is sent as a no-cache file.