In this article I will explain with an example, what is Content Disposition Header in ASP.Net?
What is Content Disposition Header?
The Content Disposition Header is used to inform the browser that the downloading file is an attachment i.e. the file must be saved on the computer and user will get an option to open or save the file.
It also allows us to set the filename for the exported file.
Note: If Content Disposition Header is not used then, the downloading file will be automatically opened in the browser.
C#
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=Sample.pdf");
Response.WriteFile(Server.MapPath("~/Sample.pdf"));
Response.Flush();
Response.End();
VB.Net
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = ContentType
Response.AppendHeader("Content-Disposition", "attachment; filename=Sample.pdf")
Response.WriteFile(Server.MapPath("~/Sample.pdf"))
Response.Flush()
Response.End()
Screenshots
Internet Explorer
With Content Disposition
Without Content Disposition