[英]Download file to browser from SFTP server in ASP.NET using SSH.NET
[英]SSH.NET : How to retrieve the status of file transfer from the SFTP server
我正在使用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));
}
}
如何檢索從服務器傳回的狀態? 我需要知道文件是否已成功傳輸。
TRY ... CATCH可以從服務器取回狀態嗎?
謝謝,
嘗試以此替換您的UploadFile行。 這提供了您正在調用的函數的回調。 回調在方括號中,其中o為ulong。 可能是寫入的字節數或百分比。
client.UploadFile(fileStream, Path.GetFileName(uploadfile), (o) =>
{
Console.WriteLine(o);
});
編輯:
以上等同於此:
//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);
他們正在調用您的函數,如果不向其傳遞值,則無法調用您的函數。
有兩個可行的選擇。
使用UploadFile(Stream input, string path, Action<ulong> uploadCallback)
的Action<ulong> uploadCallback
參數UploadFile(Stream input, string path, Action<ulong> uploadCallback)
。 這可用於檢查已上傳的字節數,並可與要發送的文件大小進行比較。
在已上傳文件的路徑上使用SftpFileSytemInformation GetStatus(string path)
,以檢查文件是否在磁盤上再次存在其大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.