使用 WebClient.UploadFileAsync 时,文件未上传到某些计算机上的 FTP 服务器

File is not uploaded to FTP server on some machines when using WebClient.UploadFileAsync

我正在尝试将文件上传到 FileZilla FTP 服务器。我在我的电脑上安装了服务器,我正在使用一个简单的 C# 脚本来上传文件。当我 运行 来自同一台机器的脚本时它工作正常。当我尝试从同一或不同 LAN 的另一台计算机 运行 脚本时,我遇到的问题是:

 (not logged in) (ClientIP)> USER userID
 (not logged in) (ClientIP)> 331 Password required for userID
 (not logged in) (ClientIP)> PASS ***************
  userID (ClientIP)> 230 Logged on
  userID (ClientIP)> OPTS utf8 on
  userID (ClientIP)> 202 UTF8 mode is always enabled. No need to send this command.
  userID (ClientIP)> PWD
  userID (ClientIP)> 257 "/" is current directory.
  userID (ClientIP)> TYPE I
  userID (ClientIP)> 200 Type set to I
  userID (ClientIP)> PASV
  userID (ClientIP)> 227 Entering Passive Mode (ClientIP)

这里的问题可能是由什么引起的?我从自己的 PC 收到的消息如下:

(not logged in) (pcIP)> USER userID
(not logged in) (pcIP)> 331 Password required for userID
(not logged in) (pcIP)> PASS ***************
userID (pcIP)> 230 Logged on
userID (pcIP)> OPTS utf8 on
userID (pcIP)> 202 UTF8 mode is always enabled. No need to send this command.
userID (pcIP)> PWD
userID (pcIP)> 257 "/" is current directory.
userID (pcIP)> TYPE I
userID (pcIP)> 200 Type set to I
userID (pcIP)> PASV
userID (pcIP)> 227 Entering Passive Mode ((pcIP))
userID (pcIP)> STOR myTempDoc.pdf
userID (pcIP)> 150 Opening data channel for file upload to server of "/myTempDoc.pdf"
userID (pcIP)> 226 Successfully transferred "/myTempDoc.pdf"

唯一不同的是,在第一种情况下我无法上传所需的文件。这里有什么不同?

uploadX(string path)
{
    string host = "ftp://ip:port/";
    string user = "userID";
    string pass = "password";
    WebClient Xclient = new System.Net.WebClient();
    Uri uri = new Uri(host + new FileInfo(path).Name);
    Xclient.Credentials = new System.Net.NetworkCredential(user, pass);
    Xclient.UploadFileAsync(uri, "STOR", path);
}

在我的主要函数中调用 uploadX("docName")

您是否检查过防火墙以确保数据端口已打开?

由于正在切换 PASV 模式,FTP 服务器应该发送一个新的端口进行通信。通常这些数据端口是 1024 到 5000。

关闭 Windows 防火墙,看看是否能解决问题。如果是这样,请在防火墙中打开上述端口或告诉 FileZilla 使用设置中的一组特定端口并打开这些端口。

您没有在等待上传完成。因此,如果这是一个在 uploadX returns 之后很快退出的小型独立脚本,上传可能根本没有完成,在 script/application 完成之前。这或许可以解释不同机器上的随机行为。

确保等待上传完成。通过捕获 UploadFileCompleted event, or simply by using a blocking UploadFile method.