Thursday, June 17, 2010

Code for Download a file from a folder

string[] filepath = Directory.GetFiles(Server.MapPath("~\\upload files"));
            foreach (string fileName in filepath)
            {
                string filename_read = Path.GetFileNameWithoutExtension(fileName);
                if (hidden_tar_file.Value == filename_read)
                {
                    System.IO.FileInfo file = new System.IO.FileInfo(fileName);
                    if (file.Exists)
                    {
                        Response.Clear();
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                        Response.AddHeader("Content-Length", file.Length.ToString());
                        Response.ContentType = "application/octet-stream";
                        Response.WriteFile(file.FullName);
                        Response.End();
                    }
                }
         
            }

No comments: