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.

  1. Hi.

    When you change to

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

    the document open in the browser, but I need it open in application, you know how make that??.

    Sorry for my English is very bad :( .

  2. You're best chance at having this happen is to add a line as follows:

    Response.AddHeader("content-type", "application/octet-stream")

    This will force the "Save As/Open With" Dialog box to show up.

  3. Hello, I have a problem with this process. When I publish my App in a Productive Server, the process is very slow.
    And the problem is in the rendering directives. But i don't know how accelerate the rendering to excel process.
    Please, sendme a help to relhaibe@gmail.com

  4. Hi Rodrigo

    I was going through your code at http://www.parallelcoding.com/2008/05/30/content-disposition-attachment-vs-inline/

    I have a different problem and I need you help:-)

    In my case, I can force it to open a 'Save as' window. But my new requirement is I need to send this file as an attachment to an email id using CDO object.

    Do you have any idea? Please let me know.

    My present code to force to open a 'Save As' is;

    var FileName = xmlResponse.selectSingleNode("//d:FileName").text;
    var ContentType = xmlResponse.selectSingleNode("//d:ContentType").text;
    var FileContents = xmlResponse.selectSingleNode("//d:FileContents");
    var BinaryData = Base64Decode(FileContents.text);

    Response.ContentType = ContentType;
    Response.AddHeader ("Content-Disposition", "attachment; filename=" + escape(FileName));

    Response.BinaryWrite (BinaryData);
    Response.flush();
    Response.end;

    Thanks
    Balaji

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">