Hi, I want to select a directory and upload all the files in that to the server using asp.net..... is there anyway to do that...... if anyone knows please tell me the code... its too urgent... i have to submit it on this week...Pleaseeeeeeee
Use Follow Code: The Controls are Treeview,Gridview using System; using System.IO; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } private void FillDirectory(TreeNode parent, string strPath) { DirectoryInfo dr = new DirectoryInfo(strPath); if (dr.Exists) { foreach (DirectoryInfo d in dr.GetDirectories()) { TreeNode node = new TreeNode(); node.Text = d.Name; node.Value = d.FullName; node.PopulateOnDemand = true; node.SelectAction = TreeNodeSelectAction.SelectExpand; parent.ChildNodes.Add(node); } } } protected void DirTreeview_TreeNodePopulate(object sender, TreeNodeEventArgs e) { string selectedstring = e.Node.Value; string strFileName = Path.Combine(currentFolderPath, selectedstring); FillDirectory(e.Node, strFileName); //BindDirContents(e.Node, gridviewFiles); } public String currentFolderPath { get { if (ViewState["m_currentFolderPath"] != null) return ViewState["m_currentFolderPath"].ToString().Trim(); else return string.Empty; } set { ViewState["m_currentFolderPath"] = value; } } private static void BindDirContents(TreeNode node, System.Web.UI.WebControls.GridView gridview) { // bind the gridview to the datasource gridview.DataSource = new System.IO.DirectoryInfo(node.Value).GetFiles(); gridview.DataBind(); } protected void gridviewFiles_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { System.IO.FileSystemInfo item = (System.IO.FileSystemInfo)e.Row.DataItem; ImageButton imageButton = (ImageButton)e.Row.FindControl("btnItemIcon"); System.IO.FileInfo fileInfo = (System.IO.FileInfo)item; Label lblSize = (Label)e.Row.FindControl("lblSize"); lblSize.Text = string.Format("{0:N0} KB", fileInfo.Length / 1000); if ((fileInfo.Name.EndsWith(".rtf") || fileInfo.Name.EndsWith(".doc") || fileInfo.Name.EndsWith(".txt"))) { imageButton.ImageUrl = @"Image/doc.jpg"; } else { imageButton.ImageUrl = @"Image/voice.png"; } } } protected void DirTreeview_SelectedNodeChanged(object sender, EventArgs e) { BindDirContents(this.DirTreeview.SelectedNode, this.gridviewFiles); } protected void gridviewFiles_RowCommand(object sender, GridViewCommandEventArgs e) { // handle either opening the item or rebinding the grid if (e.CommandName == "ItemClick") { string name = (string)e.CommandArgument; System.IO.DirectoryInfo dinfo = new System.IO.DirectoryInfo(this.DirTreeview.SelectedNode.Value); if (System.IO.File.Exists(System.IO.Path.Combine(dinfo.FullName, name))) { System.IO.FileInfo fileInfo = new System.IO.FileInfo(System.IO.Path.Combine(dinfo.FullName, name)); // they clicked on a file, download it to there PC this.Response.Clear(); this.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name); this.Response.WriteFile(fileInfo.FullName); this.Response.End(); } else { foreach (TreeNode node in this.DirTreeview.SelectedNode.ChildNodes) { if (node.Text == name) { node.Selected = true; node.Expanded = true; // expand the parents TreeNode parentNode = node.Parent; while (parentNode != null) { parentNode.Expanded = true; parentNode = parentNode.Parent; } // bind the gridview to the datasource BindDirContents(node, this.gridviewFiles); break; } } } } } }