I have an asp.net application where the user can download a file by clicking a link. It could be a file of any type. Getting it work took more effort than expected. Let me use my blog as a public notepad and share the result.
protected void LinkButtonDownLoad_Click(object sender, EventArgs e)
{
string bestandsnaam = (sender as LinkButton).CommandArgument;
string fullFileName = Server.MapPath("DownLoads") + "\\" + bestandsnaam;
if (System.IO.File.Exists(fullFileName))
{
bestandsnaam = Server.UrlPathEncode(bestandsnaam);
Response.Clear();
Response.ContentType = "application/binary";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", bestandsnaam));
Response.TransmitFile(fullFileName);
Response.Flush();
Response.End();
}
}