File download dialog IE7 disappears

2019-05-21 11:58发布

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

5条回答
淡お忘
2楼-- · 2019-05-21 12:26

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-05-21 12:34

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.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-05-21 12:40

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.

查看更多
姐就是有狂的资本
5楼-- · 2019-05-21 12:47

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

查看更多
冷血范
6楼-- · 2019-05-21 12:48

Had this problem on our intranet, automatic prompting for downloads didn't work for me (was already selected) but this did...

Tools -> Internet Options

On Security tab select ‘Local intranet’ then click Sites

Click Advanced

Type “http://your.url.com” and click Add

Click Close -> Ok -> Ok

hope this works for you too ;)

查看更多
登录 后发表回答