Archive

Archive for May, 2008

Content-Disposition attachment vs inline

May 30, 2008 13 comments

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:

  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

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

to

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.

Categories: .NET, ASP