【问题标题】:Upload directory of files to FTP server using WebClient使用 WebClient 将文件目录上传到 FTP 服务器
【发布时间】:2017-05-16 22:30:48
【问题描述】:

我已经搜索和搜索,但找不到这样做的方法。我在要上传的目录中有文件。文件名不断变化,所以我无法按文件名上传。这是我尝试过的。

using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential("User", "Password");
    foreach (var filePath in files)
        client.UploadFile("ftp://site.net//PICS_CAM1//", "STOR", @"PICS_CAM1\");
}

但是我得到一个编译器错误:

当前上下文中不存在名称“文件”

我研究过的一切都表明这应该有效。

有没有人有通过WebClient 上传文件目录的好方法?

【问题讨论】:

    标签: c# .net ftp webclient


    【解决方案1】:

    您必须定义和设置files。如果您想上传某个本地目录中的所有文件,例如使用Directory.EnumerateFiles

    此外,WebClient.UploadFileaddress 参数必须是目标文件的完整 URL,而不仅仅是目标目录的 URL。

    IEnumerable<string> files = Directory.EnumerateFiles(@"C:\local\folder");
    
    using (WebClient client = new WebClient())
    {
        client.Credentials = new NetworkCredential("username", "password");
    
        foreach (string file in files)
        {
            client.UploadFile(
                "ftp://example.com/remote/folder/" + Path.GetFileName(file), file);
        }
    }
    

    有关递归上传,请参阅:
    Recursive upload to FTP server in C#

    【讨论】:

      【解决方案2】:

      我认为您的网络客户端上传工作正常。您的问题是您的变量 files 不在范围内。

      您需要发布更多代码,以便我们看得更清楚

      【讨论】: