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

a 夏天 提交于 2019-12-08 07:35:47

问题


I am trying to upload files to a FileZilla FTP server. I have setup in my PC the server and I am using a simple C# script in order to upload files. It works properly when I run the script from the same machine. When I tried to run the script from another computer from the same or different LAN I got issues which are:

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

What could be caused the issues here? The message I am receiving from my own PC is the following:

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

The only difference is that in the first case I cant upload the desired file. What could be the difference here?

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

And in my main function I call uploadX("docName").


回答1:


You are not waiting for the upload to complete. So if this is a small standalone script that exits soon after the uploadX returns, the upload may not finish at all, before the script/application completes. That might explain the random behavior on different machines.

Make sure you wait for the upload to complete. Either by capturing UploadFileCompleted event, or simply by using a blocking UploadFile method.




回答2:


Have you checked the firewalls to make sure the data ports are open?

Since it is switching PASV mode, the FTP server should be sending a new port to communicate on. Typically those data ports are 1024 to 5000.

Turn off the Windows firewalls to see if it fixes the issue. If it does, open the above ports in the firewall or tell FileZilla to use a specific set of ports in the settings and open those ports.



来源:https://stackoverflow.com/questions/33869019/file-is-not-uploaded-to-ftp-server-on-some-machines-when-using-webclient-uploadf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!