Tuesday, July 19, 2011

How to force a file to download in ASP.NET

Sometime you may want to force the user to download a particular file even though browser can open that file type because of any of your business need. This can be easily achieved by adding the content-disposition header in the response of your page. protected void Page_Load(object sender, EventArgs e) { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=.gif"); Response.ContentType = "image/GIF"; Response.WriteFile(Server.MapPath(@"~/computer.gif")); Response.End(); }