0 votes
by (900 points)

How to get FXP transfer progress?

{
Ftp ftp = new Ftp();
Ftp ftp2 = new Ftp();
ftp.LogWriter = new FileLogWriter("D:\\temp.txt", LogLevel.Debug);

ftp.Connect("192.168.1.143", 21);
ftp2.Connect("192.168.1.143", 21);

ftp.Login("testuser", "testuser");
ftp2.Login("testuser", "testuser");

ftp.TransferProgressChanged += ftp_TransferProgressChanged;
ftp2.TransferProgressChanged += ftp_TransferProgressChanged;

ftp.CopyToAnotherServer(ftp2, "/2/1.file", "/1/1.file");

Console.WriteLine("File Upload");
}

static void ftp_TransferProgressChanged(object sender, FtpTransferProgressChangedEventArgs e)
{
Console.WriteLine(e.BytesTransferred); //this code never execute
}

Fxp transfer between to two ftp server, client is not involved. but any workaround are there to get progress?

any suggestion, any workaround?

Applies to: Rebex FTP/SSL

1 Answer

0 votes
by (58.9k points)

The Ftp.TransferProgressChanged event notifies you about data transfer between the Rebex.Net.Ftp client and the server to which the client is connected. It will not get fired in case of server-to-server transfer as the data are transferred directly between those two FTP servers so there is no transfer between client and server involved.

There is a workaround - connect to the target FTP server with another FTP connection and then monitor the current file size via Ftp.GetFileLength method as you wish.

A simple code example is available here:

void PerformFxpTransferAndReportItsProgress()
{
    string fileName = "file.txt";
    string fileNameLocal = @"c:\" + fileName;

    string primaryServerDest = "/uploadDir1/" + fileName;
    string secondaryServerDest = "/uploadDir2/" + fileName;

    Ftp ftp1 = new Ftp();
    ftp1.Connect("server1");
    ftp1.Login("user1", "password1");

    Ftp ftp2 = new Ftp();
    ftp2.Connect("server2");
    ftp2.Login("user2", "password2");

    // get file size on source FTP server
    long fullLength = ftp1.GetFileLength(primaryServerDest);

    // connect to the target FTP server with another client - it will be used to periodacally check for file size during the time 
    // when server-to-server file transfer is in progress
    Ftp ftp3 = new Ftp();
    ftp3.Connect("server2");
    ftp3.Login("user2", "password2");

    Task task;
    // perform the server-to-server FXP transfer
    task = ftp1.CopyToAnotherServerAsync(ftp2, primaryServerDest, secondaryServerDest).ContinueWith(
        (e) =>
        {
            Console.WriteLine("File '{0}' has been transferred via FXP.", fileName);
            return;
        }
    );

    // report progress during FXP operation
    while (!task.IsCompleted)
    {
        long lengthCurrent = 0;
        if (ftp3.FileExists(secondaryServerDest))
            lengthCurrent = ftp3.GetFileLength(secondaryServerDest);

        Console.WriteLine("FXP transfer progress: {0}B out of {1}B has been uploaded.", lengthCurrent, fullLength);
        Thread.Sleep(500); // choose your own interval
    }
}
...