简体   繁体   English

SSH.NET:如何从SFTP服务器检索文件传输的状态

[英]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: 我正在使用SSH.NET库,并编写了一种将文件ftp-ing到服务器的简单方法,如下所示:

    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? TRY ... CATCH可以从服务器取回状态吗?

Thank you, 谢谢,

Try replacing your UploadFile line with this. 尝试以此替换您的UploadFile行。 This provides a callback to the function you are calling. 这提供了您正在调用的函数的回调。 The callback is in the brackets with o being a ulong. 回调在方括号中,其中o为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) . 使用UploadFile(Stream input, string path, Action<ulong> uploadCallback)Action<ulong> uploadCallback参数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. 在已上传文件的路径上使用SftpFileSytemInformation GetStatus(string path) ,以检查文件是否在磁盘上再次存在其大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 SSH.NET 从 ASP.NET 中的 SFTP 服务器下载文件到浏览器 - Download file to browser from SFTP server in ASP.NET using SSH.NET 使用 SSH.NET 将数据从内存上传到 SFTP 服务器 - Upload data from memory to SFTP server using SSH.NET 使用SSH.NET从SFTP服务器下载一个特定文件 - Download one specific file from SFTP server using SSH.NET 在尝试使用 SSH.NET 提取从 SFTP 服务器下载的 ZIP 文件后获取“它正在被另一个进程使用” - Getting "it is being used by another process" after trying to extract a ZIP file downloaded from SFTP server using SSH.NET 将通过 HTTP 上传到 ASP.NET 的文件进一步上传到 C# 和 SSH.NET 中的 SFTP 服务器 - Upload file uploaded via HTTP to ASP.NET further to SFTP server in C# and SSH.NET 使用 SSH.NET 将 csvhelper 创建的内容的内存流上传到 SFTP 服务器时,上传的文件为空 - When uploading memory stream with contents created by csvhelper using SSH.NET to SFTP server, the uploaded file is empty 使用 SSH.NET 从 Ubuntu 服务器上的文件中删除行 - Delete lines from a file on Ubuntu server using SSH.NET SSH.Net 上传文件到 SFTP 服务器返回异常并显示“失败”消息,没有明确的详细信息 - SSH.Net Upload file to SFTP server returns exception with "Failure" message without clear details 使用SSH.NET将文件上传到SFTP:无法连接 - Uploading file to SFTP using SSH.NET: Cannot connect SSH.NET:是否可以使用SFTP上传文件并保留源文件中的文件日期? - SSH.NET: Is it possible to upload files using SFTP and preserve the file dates from source files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM