Search code examples
c#ftpwebclientfilezilla

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


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").


Solution

  • 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.