views:

2647

answers:

5

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

+2  A: 

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.

scunliffe
Issue solved by adjusting the settings in the IE for handling pop-ups from a specific host.Thanks for the input anyway
Greco
A: 

I still can not get the dialog box to not disappear. This happens when the application accesses a [webmethod] to gather information to pass to the page that is trying download the results to excel.

            Response.Clear();

            Response.ClearHeaders();
            Response.ClearContent();

            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileNameToUse + "\"");
             Response.CacheControl = "Public";

            Response.Write(output);
            Response.Flush();
            Response.Close();

This also only appears to happen when accessing the web sit using an ip address (999.11.1.111\default.aspx....). It works when accessing it via loacalhost\default.aspx

A: 

To test if security is causing this issue, set the target to _self. The warning bar on top of IE should appear. If this is the cause, check IE security. Specifically the prompt for download part.

A: 

Issue solved by adjusting the settings in the IE for handling pop-ups from a specific host. Thanks for the input anyway – Greco Jan 8 at 10:21

Would you mid share your solution? How and what to adjust the settings?

Thanks

+2  A: 

I had the same issue, and spent an hour being utterly frustrated. As usual microsoft IE browsers are the root of all headaches. Everything worked fine in other browsers. The solution is simple: The user will have to adjust an IE7 setting by going to 'Tools' > 'Internet Options' > 'Security' Tab > For 'Internet' and/or 'Local intranet' adjust the security settings by clicking the button, 'Custom level ...'

Go to the 'Downloads' node '> Automatic prompting for file downloads' > check 'Enable'

That fixed it for my users.

Hope that helps.