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

我正在尝试将文件上传到FileZilla FTP服务器。 我在我的电脑上设置了服务器,我使用一个简单的C#脚本来上传文件。 当我从同一台机器运行脚本时,它可以正常工作。 当我尝试从相同或不同的局域网中的另一台计算机运行该脚本时,我遇到了以下问题:

(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) 

什么可能导致这里的问题? 我从自己的电脑收到的消息如下:

 (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); } 

在我的主要function中,我调用uploadX("docName")

您不是在等待上传完成。 因此,如果这是一个在uploadX返回后很快退出的小型独立脚本,则在脚本/应用程序完成之前,上载可能根本无法完成。 这可能解释了不同机器上的随机行为。

确保您等待上传完成。 通过捕获UploadFileCompleted事件 ,或者只是使用阻塞的UploadFile方法 。

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

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

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