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.
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
.
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.
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”)
This page doesn’t display correctly in Google Chrome. The code text is clipped.
Please visit my updated site at http://www.parallelcoding.com/2008/05/30/content-disposition-attachment-vs-inline/
For clipping try
header(“Content-Length: $size”);
It worked for me (sorry for the PHP)
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.
thanks. it helped to solve my problem with pdf
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()
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.
Hi,
Does anyone knows how to make it work on IE https?
Thanks, I was having the same problem with IE.