Home > .NET, ASP > Content-Disposition attachment vs inline

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:

  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.

Advertisements
Categories: .NET, ASP
  1. Milagros
    June 26, 2008 at 9:34 pm

    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. relic411
    June 27, 2008 at 12:20 pm

    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. Azhar Khan
    January 14, 2009 at 9:17 pm

    You can leave the content-disposition as attachment, all you need to do is replace the following line:

    Response.Cache.SetCacheability(HttpCacheability.NoCache)

    with the following:

    Response.AddHeader(“cache-control”, “must-revalidate”)

  4. Raj Parmar
    March 26, 2009 at 6:36 am

    This page doesn’t display correctly in Google Chrome. The code text is clipped.

  5. BugMan
    November 30, 2009 at 10:02 pm

    For clipping try

    header(“Content-Length: $size”);

    It worked for me (sorry for the PHP)

  6. Douglas Smith
    October 22, 2010 at 3:49 pm

    from: http://stackoverflow.com/questions/1395151/content-dispositionwhat-are-the-differences-between-inline-and-attachment

    This behavior depends on the browser and the file you are trying to serve. With inline, the browser will try to open the file within the browser.

    For example, if you have a PDF file and Firefox/Adobe Reader, an inline disposition will open the PDF within Firefox, whereas attachment will force it to download.

    If you’re serving a .ZIP file, browsers won’t be able to display it inline, so for inline and attachment dispositions, the file will be downloaded.

  7. November 15, 2010 at 7:56 pm

    thanks. it helped to solve my problem with pdf

  8. Brian
    December 2, 2010 at 7:36 pm

    Anyone who can help with below code when populate to ms Excel with competible version.

    When i ran below code, ms excel always asked that the verison is different, how can i disable that ms excell message pop up.

    Again, appreciate very for your help

    Dim ds As DataSet = Session(“items”)
    Response.Clear()
    Response.Buffer = True
    Response.ContentType = “Application/x-msexcel”
    Response.AddHeader(“Content-Disposition”, “inline;filename=ItemWithBadLocation.xlsx”)
    Response.Charset = “”
    Dim DV As New DataView(ds.Tables(0))
    Response.Write(ExportDataToExcel(DV))
    Response.End()

  9. brock
    April 10, 2011 at 7:10 pm

    For Browsers the inline / attachment values should be interpreted as :

    – inline – display file in the current document

    – attachment – download file or display in external program

    See :
    RFC 2183 – Internet Engineering Task Force
    http://www.ietf.org/rfc/rfc2183.txt
    Communicating Presentation Information in Internet Messages: The Content-Disposition Header Field

    And :
    Force Download with HTTP Headers
    http://www.symkat.com/tag/rfc-2183/

    Excerpt from RFC 2183 :

    2.1 The Inline Disposition Type

    A bodypart should be marked `inline’ if it is intended to be
    displayed automatically upon display of the message. Inline
    bodyparts should be presented in the order in which they occur,
    subject to the normal semantics of multipart messages.

    2.2 The Attachment Disposition Type

    Bodyparts can be designated `attachment’ to indicate that they are
    separate from the main body of the mail message, and that their
    display should not be automatic, but contingent upon some further
    action of the user. The MUA might instead present the user of a
    bitmap terminal with an iconic representation of the attachments, or,
    on character terminals, with a list of attachments from which the
    user could select for viewing or storage.

  10. Anat
    June 1, 2011 at 6:42 am

    Hi,

    Does anyone knows how to make it work on IE https?

  11. Bryan Willman
    December 4, 2011 at 11:32 pm

    Thanks, I was having the same problem with IE.

  1. April 29, 2010 at 9:29 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: