Force a file to download in asp.net c#
ASP.NET July 5, 2013 Comments Off on Force a file to download in asp.net c#In web applications you may need to download image, zip files, doc files, pdf files, text files. Luckily, Pdf files zip files, doc, docx files will download but images will be displayed by browsers.
To force any file to download, we can use the following snippet to. Assuming the myphoto.jpg is stored in the datastore folder located in the root.
string FileName = "myphoto.jpg"; Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "text/plain"; Response.AddHeader("content-disposition", "attachment;filename=" + FileName); Response.TransmitFile(Server.MapPath("~/datastore/" + FileName)); Response.End();