M. Fawad Surosh
M. Fawad Surosh

Reputation: 480

SSH.NET : How to retrieve the status of file transfer from the SFTP server

I am using SSH.NET library and have written a simple method for ftp-ing files to a server as below:

    using (var client = new Renci.SshNet.SftpClient(host, port, username, password))
    {
        client.Connect();
        Console.WriteLine("Connected to {0}", host);

        using (var fileStream = new FileStream(uploadfile, FileMode.Open))
        {
            client.BufferSize = 4 * 1024; // bypass Payload error large files
            client.UploadFile(fileStream, Path.GetFileName(uploadfile));
        }
    }

How can I retrieve the status of transfer back from the server? I need to know if the files are being transferred successfully.

Can a TRY...CATCH work to retrieve the status back from the server?

Thank you,

Upvotes: 0

Views: 6739

Answers (2)

zzxyz
zzxyz

Reputation: 2981

Try replacing your UploadFile line with this. This provides a callback to the function you are calling. The callback is in the brackets with o being a ulong. Probably a percentage or number of bytes written.

        client.UploadFile(fileStream, Path.GetFileName(uploadfile), (o) =>
        {
            Console.WriteLine(o);
        });

EDIT:

The above is equivalent to this:

//I might be called multiple times during the upload process.
public void OnStatusUpdate(ulong bytesWritten)
{
    Console.WriteLine(bytesWritten);
}
...
    //later
    client.UploadFile(fileStream, Path.GetFileName(uploadfile), OnStatusUpdate);

They are calling YOUR function, and your function cannot be called without a value being passed to it.

Upvotes: 2

William Denman
William Denman

Reputation: 3154

There are two options that could work.

  1. Use the Action<ulong> uploadCallback parameter of UploadFile(Stream input, string path, Action<ulong> uploadCallback). This can be used to check the number of bytes that been uploaded and could be compared with the size of the file you are sending.

  2. Use SftpFileSytemInformation GetStatus(string path) on the path of the file you have uploaded, to check whether the file exists an again its size on disk.

Upvotes: 0

Related Questions