如何使用ftplib将目录上传到ftp?

时间:2021-12-30 04:03:15

I have problem with upload all files to ftp: I use ftplib. I have a function to upload:

我将所有文件上传到ftp都有问题:我使用ftplib。我有一个上传功能:

static void DirSearch(string sDir, FtpConnection ftp)
{
  try
  {
    foreach (string d in Directory.GetDirectories(sDir))
    {
      string dirname = new DirectoryInfo(d).Name;
      if (!ftp.DirectoryExists(dirname))
      {
        ftp.CreateDirectory(dirname);
      }
      ftp.SetCurrentDirectory(dirname);
      foreach (string f in Directory.GetFiles(d))
      {
        Uri uri = new Uri(f);            
        ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));

      }
      DirSearch(d, ftp);
    }
  }
  catch (System.Exception e)
  {
    MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error);
  }
}

ok this function uload files but I have in local disc files:

好这个函数uload文件,但我有本地光盘文件:

UPLOAD
--DIR1
----DIR3
------FILE4
----FILE3
--DIR2
----DIR4
------FILE7
----FILE5
----FILE6
--FILE1
--FILE2

In serwer I have:

在服务中我有:

UPLOAD
--DIR1
----DIR3
------DIR2
--------DIR4
----------FILE7
--------FILE5
--------FILE6
------FILE4
----FILE3

I dont have files in first folder and dir tree is wrong i think foult is in line ftp.SetCurrentDirectory(dirname);

我没有第一个文件夹中的文件和dir树是错误的我觉得foult在线ftp.SetCurrentDirectory(dirname);

3 个解决方案

#1


2  

Well, your function is the problem - when you enter the folder, and copy the files into it, you are not going back to the previous folder, instead you are going more deeply into tree.

好吧,你的功能是问题 - 当你进入文件夹,并将文件复制到它,你不会回到上一个文件夹,而是你更深入到树。

Simple solution for this is to rewrite this function to go back from the directory once it has iterated through it:

对此的简单解决方案是重写此函数,以便在迭代通过目录后从目录返回:

static void DirSearch(string sDir, FtpConnection ftp)
{
  try
  {
     // First, copy all files in the current directory
     foreach (string f in Directory.GetFiles(d))
     {
       Uri uri = new Uri(f);            
       ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));
     }

    // For all directories in the current directory, create directory if there is
    // no such, and call this function recursively to copy files.

    foreach (string d in Directory.GetDirectories(sDir))
    {
      string dirname = new DirectoryInfo(d).Name;
      if (!ftp.DirectoryExists(dirname))
      {
        ftp.CreateDirectory(dirname);
      }
      ftp.SetCurrentDirectory(dirname);
      DirSearch(d, ftp);
    }



  }
  catch (System.Exception e)
  {
    MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error);
  }
  finally{
     // Go back! 
     ftp.SetCurrentDirectory(".."); //untested, but it should be fine, as I don't see cdup command in ftplib
  }

}

#2


1  

Yes, you're right. You can save and assign the current directory on each call. Try this:

你是对的。您可以在每次调用时保存并分配当前目录。尝试这个:

static void DirSearch(string sDir, FtpConnection ftp, string currentDirectory)
{
  try
  {
    ftp.SetCurrentDirectory(currentDirectory);
    foreach (string d in Directory.GetDirectories(sDir))
    {
      string dirname = new DirectoryInfo(d).Name;
      if (!ftp.DirectoryExists(dirname))
      {
        ftp.CreateDirectory(dirname);
      }      
      foreach (string f in Directory.GetFiles(d))
      {
        Uri uri = new Uri(f);            
        ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));

      }
      string newCurrentDir = currentDirectory + dirname + "/";
      DirSearch(d, ftp, newCurrentDir);
    }
  }
  catch (System.Exception e)
  {
    MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error);
  }
}

and method calling

和方法调用

DirSearch("your initial dir", your ftp connection, "/");

#3


0  

This code is good

这段代码很好

static void DirSearch(string sDir, FtpConnection ftp, string currentDirectory)
{
    try
    {
        ftp.SetCurrentDirectory(currentDirectory);
        foreach (string f in Directory.GetFiles(sDir))
        {
            Uri uri = new Uri(f);
            ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));

        }
        foreach (string d in Directory.GetDirectories(sDir))
        {
            ftp.SetCurrentDirectory(currentDirectory);
            string dirname = new DirectoryInfo(d).Name;
            if (!ftp.DirectoryExists(dirname))
            {
                ftp.CreateDirectory(dirname);
            }

            string newCurrentDir = currentDirectory + "/" + dirname ;
            DirSearch(d, ftp, newCurrentDir);
        }
    }
    catch (System.Exception e)
    {
        MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error);
    }
}

#1


2  

Well, your function is the problem - when you enter the folder, and copy the files into it, you are not going back to the previous folder, instead you are going more deeply into tree.

好吧,你的功能是问题 - 当你进入文件夹,并将文件复制到它,你不会回到上一个文件夹,而是你更深入到树。

Simple solution for this is to rewrite this function to go back from the directory once it has iterated through it:

对此的简单解决方案是重写此函数,以便在迭代通过目录后从目录返回:

static void DirSearch(string sDir, FtpConnection ftp)
{
  try
  {
     // First, copy all files in the current directory
     foreach (string f in Directory.GetFiles(d))
     {
       Uri uri = new Uri(f);            
       ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));
     }

    // For all directories in the current directory, create directory if there is
    // no such, and call this function recursively to copy files.

    foreach (string d in Directory.GetDirectories(sDir))
    {
      string dirname = new DirectoryInfo(d).Name;
      if (!ftp.DirectoryExists(dirname))
      {
        ftp.CreateDirectory(dirname);
      }
      ftp.SetCurrentDirectory(dirname);
      DirSearch(d, ftp);
    }



  }
  catch (System.Exception e)
  {
    MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error);
  }
  finally{
     // Go back! 
     ftp.SetCurrentDirectory(".."); //untested, but it should be fine, as I don't see cdup command in ftplib
  }

}

#2


1  

Yes, you're right. You can save and assign the current directory on each call. Try this:

你是对的。您可以在每次调用时保存并分配当前目录。尝试这个:

static void DirSearch(string sDir, FtpConnection ftp, string currentDirectory)
{
  try
  {
    ftp.SetCurrentDirectory(currentDirectory);
    foreach (string d in Directory.GetDirectories(sDir))
    {
      string dirname = new DirectoryInfo(d).Name;
      if (!ftp.DirectoryExists(dirname))
      {
        ftp.CreateDirectory(dirname);
      }      
      foreach (string f in Directory.GetFiles(d))
      {
        Uri uri = new Uri(f);            
        ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));

      }
      string newCurrentDir = currentDirectory + dirname + "/";
      DirSearch(d, ftp, newCurrentDir);
    }
  }
  catch (System.Exception e)
  {
    MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error);
  }
}

and method calling

和方法调用

DirSearch("your initial dir", your ftp connection, "/");

#3


0  

This code is good

这段代码很好

static void DirSearch(string sDir, FtpConnection ftp, string currentDirectory)
{
    try
    {
        ftp.SetCurrentDirectory(currentDirectory);
        foreach (string f in Directory.GetFiles(sDir))
        {
            Uri uri = new Uri(f);
            ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath));

        }
        foreach (string d in Directory.GetDirectories(sDir))
        {
            ftp.SetCurrentDirectory(currentDirectory);
            string dirname = new DirectoryInfo(d).Name;
            if (!ftp.DirectoryExists(dirname))
            {
                ftp.CreateDirectory(dirname);
            }

            string newCurrentDir = currentDirectory + "/" + dirname ;
            DirSearch(d, ftp, newCurrentDir);
        }
    }
    catch (System.Exception e)
    {
        MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error);
    }
}