File Transfer Progress


** Added Issue as status PROPOSED type FEATURE **This is a discussion thread. Is there any interest in including a way to expose current transfer progress to an application? For example, imagine a user wanting to upload a file could pass in an implementation of:    public interface IFileTransferHandler{ ulong BytesRead { get; }  void IncrementBytesRead(ulong bytes);  void TransferCompleted();}    Whenever the callback for Write returns confirming that the bytes were written, IncrementReadBytes would be called and whenever the file is fully uploaded TransferCompleted gets called. Since we're passing in streams, there's no objective way to determine the file size so that would have to be provided in the implementation, if at all. All you really need to do is add a field for that interface in SftpFileStream and then pass it to all calls to RequestWrite and then create some new constructors so that the interface can be passed along from the publicly exposed methods (I've managed to build a sample implementation for sftp upload, so this method works). The only issue is if the handler throws an exception. Since it's asynchronous the debugger displays the stack trace in the wrong place and could confuse developers.
Closed Apr 6 at 2:56 PM by drieseng


nemec wrote Sep 15, 2012 at 6:24 AM

Something I noticed in the SFTP code: there are already custom AsyncResults for upload and download that seem to track the number of bytes "read" but that doesn't seem to be a useful metric for Upload. It seems to update as soon as the (asynchronous) call to Write is performed on the connection, meaning the count keeps increasing despite the fact that the data may not be actually written.

Since the asyncResult seems to do half of what the handler does (execute a callback on completion) and no one is using it for anything else, I think I'm just going to replace the AsyncCallback in the code with the IFileTransferHandler interface and craft an AsyncCallback inside SftpUploadAsyncResult that just calls a private TransferCompleted method on the AsyncResult.

Thoughts? It seems to work pretty well to me, though if the handler throws an exception it's occasionally difficult to trace where it came from. I don't think we want to swallow exceptions, but....

olegkap wrote Dec 27, 2012 at 3:18 PM


I just checked in a code where I added few callback functions which are called based on progress.
Now UploadFile, DownloadFile and ListDirectory will have extra optional callback parameter which can be a function to report progress.

Here is code example that I used to test it:
                sftp.UploadFile(File.OpenRead(@"D:\aaaaa.csv"), "test.mpg", (count) => {
Please let me know if you need any other parameters to be reported.


olegkap wrote Dec 27, 2012 at 3:19 PM

Also, I changed the logic so now it will report only number of bytes actually uploaded to the server.