0 votes
by (160 points)
edited

Hello,

I'm currently evaluating the SFTP software and am loving what I've seen so far. However, I'm unable to get UploadAsync (or DownloadAsync for that matter) to work properly. Passing a single memorystream to PutFileAsync works as expected.

Here's the code:

public async Task<Task> TransferDirectoryAsync(string localDirectory, string remoteDirectory)
    {
        bool remoteDirectoryExists = DirectoryExists(remoteDirectory);
        if (!remoteDirectoryExists)
        {
             remoteDirectoryExists = CreateDirectory(remoteDirectory); 
        }

        Task uploadTask = null;
        try
        {
            uploadTask = _sftp.UploadAsync(localDirectory, remoteDirectory, TraversalMode.MatchFilesShallow,
                TransferMethod.Copy, ActionOnExistingFiles.OverwriteAll);
            await uploadTask;
        }
        catch (Exception ex)
        {
            logger.Write("Exception thrown while transferring file: " + ex.Message);
        }
        return uploadTask;
    }

I have a TransferProgressChanged handler attached and, immediately upon making the call, that fires with a TransferProgressState.TransferCompleted event. Again, I have the exact same logic for PutFileAsync, and it works as expected. Any help would be greatly appreciated. Thanks.

Applies to: Rebex SFTP

1 Answer

+1 vote
by (58.9k points)
edited
 
Best answer

Hello,

I do not know the context in which you are executing your method. However, for me the async variant of Upload works just fine. Could you please try the code below and let me know whether it works for you?

    Sftp _sftp;

    public async Task TransferDirectoryAsync(string localDirectory, string remoteDirectory)
    {
        bool remoteDirectoryExists = _sftp.DirectoryExists(remoteDirectory);
        if (!remoteDirectoryExists)
        {
            remoteDirectory = _sftp.CreateDirectory(remoteDirectory);
        }

        Task uploadTask = null;
        try
        {
            uploadTask = _sftp.UploadAsync(localDirectory, remoteDirectory, TraversalMode.Recursive,
                TransferMethod.Copy, ActionOnExistingFiles.OverwriteAll);
            await uploadTask;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception thrown while transferring file: " + ex.Message);
        }
        return;
    }

        _sftp = new Sftp();
        _sftp.Connect("server");
        _sftp.Login("user", "pass");

        Task x = TransferDirectoryAsync(@"C:\temp\upload\*", "/home/user/upload/_asyncTest");

        x.Wait();
        _sftp.Disconnect();
by (160 points)
edited

Thomas,

What you've posted here worked just fine. From it, I've noticed that you specified "*" in the local directory path. That appears to have been my issue, for I was just passing in the directory. Thank you so much for your help with this.

-Pen

by (58.9k points)
edited

Hello Penthalis, you're welcome and I am glad to hear that everything is working for you now!

...