简体   繁体   中英

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,

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM