C# .NET - Downloading a file from server

Asked By k k on 23-Nov-12 03:53 AM
I have a ZIP file in a server location and i have to download that using c# code . can any one help me on this?
MARKAND BHATT replied to k k on 28-Nov-12 09:22 AM
Its very simple.

If you want it through html tags, below is the code for it
1.<a href="DownloadFile.zip">download File</a>

And to do it using C# code behind, lets take a button btnDownload, and under its click event paste the following code.

1.FileInfo objFileInfo = new FileInfo(Server.MapPath("~/DownloadFile.zip"));
2.Response.Clear();
3.Response.AddHeader("Content-Disposition", "attachment;filename=" + objFileInfo.Name);//Add File name to dialog display
4.Response.AddHeader("Content-Length", objFileInfo.Length.ToString());//Add the file length to dialog display
5.Response.ContentType = "application/octet-stream";
6.Response.WriteFile(objFileInfo.FullName);//download your file form service
7.Response.End();

visit https://sites.google.com/a/mbhatt.in/codesamples/dotnet-samples/c for more details