0 votes
by (170 points)

Below is the code which I am trying to achieve the requirement (using Upload method of FileTransferClient class).

Triying to trasfert file between servers without downloading at aplication side

 private void btnSendFile_Click(object sender, EventArgs e)
    {
        try
        {
            FileTransferClient client = new FileTransferClient();
            client.TransferProgressChanged += new EventHandler<TransferProgressChangedEventArgs>(TransferProgressChanged);
            client.Connect("Server", 22, FileTransferMode.Sftp);
            if (client.IsConnected == true)
            {
                client.Login("AccountName", "AccountPassword");
                if (client.IsAuthenticated == true)
                {
                    client.Upload("/Server1/home/file.txt", "/Server2/home/Downloads");
                }
            }
        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message);
        }
    }
    void TransferProgressChanged(object sender, TransferProgressChangedEventArgs e)
    {
        //do your operation
    }
Applies to: Rebex SFTP

1 Answer

0 votes
by (150k points)

Your code should work. Are you having any problems with it?
In that case, use FileTransferClient's logging API to create a debug log, which should make it possible to tell what's going on.

Some general suggestions that could be useful:
- IsConnectes will always return true after a successful Connect call, so there is no need to check it.
- IsAuthenticated will always return true after a successful Login call, so there is no need to check it either.
- Using synchronous variants of FileTransferClient methods in a click event handler is not a good idea, because it would block the application. Depending on the application type (is it a web app or a Windows app?), consider using asynchronous variants, or run the whole operation in a background thread.
- It's a good practice to disconnect or dispose the instance of FileTransferClient when it's no longer needed. Otherwise, the connection would stay open for a while until the instance is claimed by .NET's garbage collector.
- There is no need to register TransferProgressChanged if the handler is empty.

private async void btnSendFile_Click(object sender, EventArgs e)
{
    try
    {
        using (var client = new FileTransferClient())
        {
            await client.ConnectAsync("test.rebex.net", 22, FileTransferMode.Sftp);
            await client.LoginAsync("demo", "password");
            await client.UploadAsync("/Server1/home/file.txt", "/Server2/home/Downloads");
        }
    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.Message);
    }
}
ago by (170 points)
Yes, I am getting error as "Could not find source path ('D:\root\testfile.exe')" as UploadAsync method does not have any parameter to specify the source file system (source client object).

My current line of ode looks like this

 using (var client = new FileTransferClient())
        {
            await client.ConnectAsync("test.rebex.net", 22, FileTransferMode.Sftp);
            await client.LoginAsync("demo", "password");
            await client.UploadAsync("/root/testfile.exe", "/root/Desktop", TraversalMode.Recursive, TransferMethod.Copy,ActionOnExistingFiles.ThrowException);
        }
ago by (150k points)
Upload/UploadAsync method does actually accept a normal Windows path with a drive letter, just like common .NET methods such as File.OpenRead(...).

For example:
    await client.UploadAsync(@"D:\root\testfile.exe", "/root/Desktop", TraversalMode.Recursive, TransferMethod.Copy,ActionOnExistingFiles.ThrowException);

Alternatively, you can even use forward slashes instead of backslashes:
    await client.UploadAsync("D:/root/testfile.exe", "/root/Desktop", TraversalMode.Recursive, TransferMethod.Copy,ActionOnExistingFiles.ThrowException);
ago by (170 points)
edited ago by
Thanks for the update, I am trying to transfer the file from one UNIX server to Another UNIX server.

So I am passing the file path only, but the UploadAsync code is looking for the file in the same drive where my application is running, that's why it is showing the "D:"

My Code is running on a Windows Machine, and I want to transfer a file from One UNIX server to another UNIX server without downloading it into my local machine.
ago by (150k points)
Unfortunately, the SFTP protocol only facilitates client-to-server and server-to-client file transfers. It does not support server-to-server transfers at all. The only way to achieve that using the SFTP protocol would be to download the file from one Unix server to the client, and then upload it fro the client to another Unix server.

To actually perform a direct server-to-server transfer, you would have to use a completely different approach, such as using Rebex SSH Shell library to launch a remote "sftp" command at one Unix server, instructing it to upload a file to another Unix server.
...