IE, Office 2003 and Office 2007 behavior to save a file with characters like []

Recently I came across an issue where one of our customers wanted to open a file from the website and, when they get an Open/Save pop up they should click on Open and then once the file is open in Microsoft Excel 2003, should be able to save it with the original name as provided in the code.

Here is the code they were using:

Default.aspx

<%@ Page Language="C#" AutoEventWireup='true' %>
<html>

<script runat='server'>
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();

Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=filename1.csv");

Response.Write("a,b,c,d\n1,2,3,4\n");
Response.End();
}
</script>
</html>

Now here is the problem. We are able to open the file in Excel 2003. Now once the file is open and if you try to save this file by clicking on File->Save/Save As, you will get an error (provided you don't remove the special character like "[" in the filename).

Here are the screenshots when I access the page.

 

Fig 1

I click on Open. Now once the Excel file is open, try to save it without changing the name (you can change the location though)

Fig 2

So you see the file cannot be saved because it contains the character "[]" in it. The name that it will try to save is filename1[1].csv.

This happens because IE stores the file on the client machine in the location C:\Documents and Settings\<profile>\Local Settings\Temporary Internet Files\Content.IE5\5N4G3UBL (some random value). The filename will be filename1[1].csv. IE automatically adds a version subscript by default.

If you try to access the same page from a machine which has Microsoft Office 2007, you will not encounter this issue. Reason being that Office 2007 is modified to use a parentheses ( "( )" ) instead of square brackets (" [ ] ") while saving the file. And since parentheses is not a part of the above restricted set as in Fig 2 we can save the file.

So as you see here, both IE and Microsoft Office 2003 are partially at faults. IE, because it adds the [] to the file name when saving it to temporary internet files folder and Microsoft Office 2003, because it cannot save a file with special characters like [ or ] in it. Office 2007 while saving the file uses parentheses and not square brackets. So there won't be a problem when client has  Microsoft Office 2007 installed.