Monthly Archives: May 2008

Content-Disposition attachment vs inline

Today I ran into an interesting issue. We have some legacy code in .NET 1.1 that exports an HTML table to Microsoft Excel. This export occurs by simply rendering the table via Response.Write and setting the header content-disposition to "attachment; filename=FileName.xls". The original code looked something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=SalesByProductReport.xls")
Response.Charset = "utf-8"
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.ms-excel"
 
Dim stringWrite As IO.StringWriter = New System.IO.StringWriter
Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite)
 
tblTable.RenderControl(htmlWrite)
 
Response.Write(stringWrite.ToString())
Response.Flush()
Response.End()

The problem that occurred was that any user using Internet Explorer (surprise, surprise!) would get a prompt to download the file but the file would not download! The file worked properly in all other browsers. The solution is to change

1
Response.AddHeader("content-disposition", "attachment;filename=SalesByProductReport.xls")

to

1
Response.AddHeader("content-disposition", "inline;filename=SalesByProductReport.xls")

Now, why exactly does this work? I'm not sure, so if you know please tell me.