Friday, December 15, 2006

IE error when exporting from ASP.NET DataTable to Excel

I have written some code which takes the contents of an ASP.NET DataTable, turns it into tab delimited format, and writes it to the Response object so that the user is presented with an IE Open/Save dialog. Open creates a new Excel instance and displays the data within, and Save allows the user to save the file locally.

This all works perfectly over our local development LAN, but when we released it to acceptance on our remote production server the following error was displayed whenever the Open button was clicked, and a similar error on Save.

'Internet Explorer was not able to open this Internet Site. The requested site is either unavailable or cannot be found. Please try again later.'

After some poking around it seems that this has been a bug with IE (surprise, surprise) since v4.0, and occurs when no-cache is set on a page in a site with an SSL certificate.

To fix it use the following (VB) code:

Response.AddHeader("Cache-Control", "max-age=0")

A full explanation of the workaround can be found here.

No comments: