[英]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. 有两个可行的选择。
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. 这可用于检查已上传的字节数,并可与要发送的文件大小进行比较。
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.