March 2010

File Download Dialogbox With File Extention

Write below code on download button click event….
protected void btnDownload_Click(object sender, EventArgs e)
{
string imgPath = btnDownload.CommandArgument; // image path goes here…(virtual path)
System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath(imgPath));
if (file.Exists)
{
HttpContext.Current.Response.AddHeader(“Content-Disposition”, “attachment; filename=drawing”);
HttpContext.Current.Response.AddHeader(“Content-Length”, file.Length.ToString());
string strExtention = file.Extension.ToString();
//HttpContext.Current.Response.ContentType = “application/octet-stream”;
HttpContext.Current.Response.ContentType = strExtention;
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.End();
}
else
{
lblError.Text = “File Does Not Exits.”;
}
}