Need Help Connecting & Uploading & Downloading FTP Files?

Dec 19, 2012 at 12:00 PM

I downloaded your latest FtpClient and impressed with the code. I have read the examples (Source/Examples) and still unsure how to begin. I thought I could simply reference the FtpClient.dll and then type something like this:

FtpClient client = new FtpClient();
client.Connect(username, password, etc...)

client.Download(params...)

client.Upload(params...)

I've been programming C# .NET for many years and very familiar with FtpWebRequest and though your FtpClient might be as easy to code-n-go...

Help!

 

 

 

 

 

 

 

Coordinator
Dec 19, 2012 at 12:34 PM
Edited Dec 19, 2012 at 12:34 PM

Hi,

There are no Upload or Download methods. What you're interested in OpenRead(), OpenWrite() and OpenAppend(). These methods return a Stream object that you can read or write to accordingly. When you're done be sure to call the Close() method of the Stream object.

Dec 19, 2012 at 1:04 PM

I'm most interested in Async Upload/download ftp.

Q. What examples would I use?

Coordinator
Dec 19, 2012 at 1:05 PM
Edited Dec 19, 2012 at 1:06 PM

Those are not async examples. Those are the examples and methods you would use to perform downloads and uploads.

Dec 19, 2012 at 1:31 PM

I would like to connect, upload, download. list, etc... using async. Help!

 

 

Coordinator
Dec 19, 2012 at 1:37 PM

Sorry, I misread what you said. The corresponding async methods start with Begin / End, i.e., BeginOpenRead() / EndOpenRead(). They follow the IAsyncResult pattern for development. There are examples of using them however one notable difference is in GUI development you wouldn't use the ManualResetEvent. It's used in the examples to block execution of the main thread until the async operation completes. This is not necessary in an event driven GUI application unless you have a specific reason to do otherwise.

Dec 19, 2012 at 2:38 PM

Okay, thx.

Having issue using BeginConnect (Checking for Connnection up/down).

Error: No host has been specified:

        public void BeginConnect()
        {
            try
            {
                using (FtpClient conn = new FtpClient())
                {
                    //m_reset.Reset();

                    conn.Host = "ftp://ftp.mycompany.com/";
                    conn.Credentials = new NetworkCredential("", "");
                    conn.BeginConnect(new AsyncCallback(ConnectCallback), conn);

                   // m_reset.WaitOne();
                    conn.Disconnect();
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
        }

Dec 19, 2012 at 2:45 PM

Looks like the error is in the 'ConnectCallback' method...

private void ConnectCallback(IAsyncResult ar)
        {
            FtpClient conn = ar.AsyncState as FtpClient;

            try
            {
                if (conn == null)
                    throw new InvalidOperationException("The FtpControlConnection object is null!");

                conn.EndConnect(ar);

                System.Windows.Forms.MessageBox.Show("Begin Connect Successful");
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
            finally
            {
                //m_reset.Set();
            }
        }

Coordinator
Dec 19, 2012 at 4:51 PM

The Host property does not accept a URI. Try this:

conn.Host = "ftp.mycompany.com"

or if you really wanted to use a URI:

conn.Host = new Uri("ftp://ftp.mycompany.com").Host

Dec 19, 2012 at 5:02 PM

Nope, not working. I can zip-up my project and send it to you?

Coordinator
Dec 19, 2012 at 5:17 PM

I don't think so, sorry. I know that the code works, if you want more help than what I've given read the documentation about debugging. Provide a transaction log and stack trace and we can go from there.

Dec 20, 2012 at 3:51 PM

Your non-async code WORKS, but your async code throws the same error trying to connect: No host has been specified

Doesn't matter if I use conn.Host = "127.0.0.1" or conn.Host = new Uri("ftp://127.0.0.1/").Host

  

I'm testing on a localhost FTP server and remote FTP server.

1. Connect method works fine.
2. GetListing works fine.

The second part of async fails:

private void ConnectCallback(IAsyncResult ar)
{
       FtpClient conn = ar.AsyncState as FtpClient; <- NULL(conn)

try
{
if (conn == null)
         throw new InvalidOperationException("The FtpControlConnection object is null!");

conn.EndConnect(ar);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
finally
{
//m_reset.Set();
}
}