做FTP功能--文件夹,文件上传

 using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Web.UI.WebControls;
using System.IO;
using upLoadModule.Class;

 

namespace upLoadModule
{
 /// <summary>
 /// Summary description for upload.
 /// </summary>
 public class upload : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DataGrid dgShow;
  protected System.Web.UI.HtmlControls.HtmlInputFile File1;
  protected System.Web.UI.WebControls.Button btnShow;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.Button Button2;
  protected System.Web.UI.WebControls.DropDownList DropDownList1;
  protected System.Web.UI.WebControls.Button btnAddDir;
  protected System.Web.UI.WebControls.Button btnModifyDir;
  protected Microsoft.Web.UI.WebControls.TreeView TreeViewShow;
  protected System.Web.UI.WebControls.Button btnAllDelete;
  protected System.Web.UI.WebControls.Button btnDelete;
  protected System.Web.UI.WebControls.Button Button3;
  protected System.Web.UI.WebControls.Button btnSaveAs;
  protected System.Web.UI.WebControls.Button btnInto;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {   
   this.TreeViewShow.SelectedIndexChange += new Microsoft.Web.UI.WebControls.SelectEventHandler(this.TreeViewShow_SelectedIndexChange);
   this.dgShow.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgShow_PageIndexChanged);
   this.btnShow.Click += new System.EventHandler(this.btnShow_Click);
   this.Button2.Click += new System.EventHandler(this.Button2_Click);
   this.btnSaveAs.Click += new System.EventHandler(this.btnSaveAs_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void btnShow_Click(object sender, System.EventArgs e)
  {
   this.TreeViewShow.Nodes.Clear();
   string theFileName = this.File1.PostedFile.FileName;
   //string theDirPath = theFileName.Substring(0,theFileName.LastIndexOf(@"/")+1);
   string theDirPath = theFileName.Substring(0,theFileName.LastIndexOf(@"/"));

   ViewState["ViewDirPath"] = theDirPath;
   //this.dgShow.DataSource = OperationClass.GetDirectorys(theDirPath); //显示文件夹集合
//   this.dgShow.DataSource = OperationClass.GetFiles(theDirPath); //显示文件集合
//   this.dgShow.DataBind();
   BindDate(theDirPath);

   DirectoryInfo dir = new DirectoryInfo(theDirPath);
   string rootFolderName = theDirPath.Substring(theDirPath.LastIndexOf(@"/")+1);

   TreeNode rootNode = new TreeNode();
//   rootNode.Text = "根";
   rootNode.Text = rootFolderName;
   rootNode.ID = theDirPath;
   rootNode.Expanded = true;
   this.TreeViewShow.Nodes.Add(rootNode);

   AddNoteTreeView(dir,rootNode);
  }

  private void BindDate(string theDirPath)
  {   
   this.dgShow.DataSource = OperationClass.GetFiles(theDirPath); //显示文件集合
   this.dgShow.DataBind();
  }

  //Good 递归获取文件夹 成功!!!
  public void AddNoteTreeView(DirectoryInfo dir,TreeNode nodeParent)
  {
   if(dir.GetDirectories().Length > 0)
   {
    DirectoryInfo[] tempdS = dir.GetDirectories();
    foreach(DirectoryInfo d in tempdS)
    {
     TreeNode tn1=new TreeNode();
     tn1.Text = d.Name;
     tn1.ID = d.FullName;
     nodeParent.Nodes.Add(tn1);
     AddNoteTreeView(d,tn1);
    }
   }
  }

  private void TreeViewShow_SelectedIndexChange(object sender, Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs e)
  {
   TreeNode selNode =new TreeNode();
   selNode = this.TreeViewShow.GetNodeFromIndex(this.TreeViewShow.SelectedNodeIndex);
   string NodeText = selNode.Text;
   string NodePath = selNode.ID;
   
//   string theDirPath = ViewState["ViewDirPath"].ToString();
//   string upPath = theDirPath.Substring(0,theDirPath.LastIndexOf(@"/")+1);
//   string theUsePath = string.Format(@"{0}{1}/",upPath,NodeText);
   BindDate(NodePath);
  }

  public void CheckAll(object sender, System.EventArgs e)
  {
   CheckBox cbAll = (CheckBox)sender;
   if(cbAll.Text=="All")
   {
    foreach(DataGridItem dgi in dgShow.Items)
    {
     CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
     cb.Checked = cbAll.Checked;
    }
   }
  }

  private void dgShow_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
  
  }

  private void Button3_Click(object sender, System.EventArgs e)
  {
   FolderDialog openFolder = new FolderDialog();
   openFolder.DisplayDialog();
  }

  private void btnSaveAs_Click(object sender, System.EventArgs e)
  {
   #region 掩藏
//   Response.Write(appPath);
//
//   foreach(DataGridItem dgi in dgShow.Items)
//   {
//    CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
//    if(cb.Checked)
//    {
//     
//    }
//   }
//
//
//   
//   TreeNode rootNode = new TreeNode();
//   rootNode.Text = "根";
//   rootNode.Expanded = true;
//   this.TreeViewShow.Nodes.Add(rootNode);
//
//   //AddNoteTreeView(dir,rootNode);
//
//   FileControl fc = new upLoadModule.Class.FileControl();
//   fc.CreateFolder("testName");
//   
   //AddDirBy_NoteTreeView();
//
//   foreach(TreeNode tn in this.TreeViewShow.Nodes[0].Nodes)//1级别
//   {
//    FileControl fc = new upLoadModule.Class.FileControl();
//    fc.CreateFolder(tn.Text);
//    
//    FileInfo[] fileS = OperationClass.GetFiles(tn.ID);
//    foreach(FileInfo f in fileS)
//    {
//     string temp22 = f.Name;
//     string temp33 = f.FullName;
//     string NewFilePath = System.Web.HttpContext.Current.Server.MapPath
//      ("upLoad/"+f.Name).ToString();
//
//     if(!File.Exists(temp22))
//     {
//      File.Copy(f.FullName,NewFilePath,true);
//     }
//    }  
//
//    if(tn.Nodes.Count > 0)
//    {
//     foreach(TreeNode tn2 in tn.Nodes)//2级别
//     {
//      FileControl fc2 = new upLoadModule.Class.FileControl();
//      string FolderPathName2  = string.Format("{0}/{1}",tn.Text,tn2.Text);
//      fc2.CreateFolder(FolderPathName2);
//     
//
//
//
//      if(tn2.Nodes.Count > 0)//3级别
//      {
//       foreach(TreeNode tn3 in tn.Nodes)
//       {
//        FileControl fc3 = new upLoadModule.Class.FileControl();
//        string FolderPathName3  = string.Format("{0}/{1}/{2}",tn.Text,tn2.Text,tn3.Text);
//        fc3.CreateFolder(FolderPathName3);
//       }
//      }
//    
//     }
//    }
//   }
  #endregion

   #region 创层次文件夹成功!!!
   TreeNode tnRoot = this.TreeViewShow.Nodes[0];
   FileControl fc = new upLoadModule.Class.FileControl();
   string temp = tnRoot.ID.ToString().Trim();
   string[] splitStr = temp.Split(new char[]{'//'});
   int sit = splitStr.Length - 1;
   string rootPath = splitStr[sit].ToString(); //获取上传文件夹的根名称
   fc.CreateFolder(rootPath);
   CopyUpLoadField(tnRoot.ID,rootPath);  //复制根目录下的 文件集合
   AddDirBy_NoteTreeView(tnRoot,rootPath);  //递归复制子目录下的 文件集合
   #endregion
   

  }
//------------------------------
  //根据客户的文件夹, 创建文件夹
  public void AddDirBy_NoteTreeView(TreeNode tn,string rootDirName)
  {
   if(tn.Nodes.Count > 0)
   {
    foreach(TreeNode tn2 in tn.Nodes)
    {
     FileControl fc2 = new upLoadModule.Class.FileControl();
     string temp2 = tn2.ID.ToString();
     string thePath= temp2.Substring(temp2.IndexOf(rootDirName),temp2.Length - temp2.IndexOf(rootDirName));
     fc2.CreateFolder(thePath);
     CopyUpLoadField(tn2.ID,rootDirName); // 复制文件夹 下的文件
     AddDirBy_NoteTreeView(tn2,rootDirName);
    }
   }
  }

//--------------------------复制文件
  public void CopyUpLoadField(string dirPath,string rootDirName)//dirPath == textNode.Id (tn2.ID)
  {
   FileInfo[] fileS = OperationClass.GetFiles(dirPath);//tn2.ID 节点文件夹的路径
   foreach(FileInfo f in fileS)
   {
    string oldFileName = f.Name;
    string oldFilePath = f.FullName;
      
    if(!File.Exists(oldFileName))
    {
     string NewFileAA = oldFilePath.Substring(oldFilePath.IndexOf(rootDirName),oldFilePath.Length - oldFilePath.IndexOf(rootDirName));
     string NewFilePath = System.Web.HttpContext.Current.Server.MapPath("upLoad/"+NewFileAA).ToString();
     try
     {
      File.Copy(oldFilePath,NewFilePath,true);
     }
     catch(System.Exception eFile)
     {
      string strE = eFile.ToString();
      string tempKK = string.Format("<script lanuage = 'javascript'>alert('文件:{0}上传失败!')</script>",@oldFilePath);
      Response.Write(tempKK);
     }
    }
   }  

  }

  private void Button2_Click(object sender, System.EventArgs e)
  {
   
  }
 }
//------------------------------------------------------
 public class OperationClass
 {
  ///   <summary>  
  ///   获取文件集合  
  ///   </summary>  
  ///   <param   name="fullPath">当前目录路径</param>  
  ///   <returns></returns>  
  public static   FileInfo[]   GetFiles(string fullPath)  
  {  
   DirectoryInfo   curDir   =   new   DirectoryInfo(fullPath);  
   FileInfo[]   fileInfo   =   curDir.GetFiles();  
   return   fileInfo;  
  } 
 
  //获取文件夹集合
  public static DirectoryInfo[] GetDirectorys(string fullPath)
  {
   DirectoryInfo   curDir   =   new   DirectoryInfo(fullPath);
   DirectoryInfo[] dirInfo = curDir.GetDirectories();
   return dirInfo;
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值