How to Track Progress of SFTP File Upload/Download in C#.NET
Today in this article, we will see how to track progress of SFTP operation using C#.NET code.
We can easily display the progress of file upload or download in a progress bar with SSH.NET library-supported methods required for the SFTP operations.
We will explore this option using the SSH.NET library for .NET/.NET Core.
Getting access to all the files using the SSH.NET library is pretty simple.
Getting Started
Create any .NET Core application like a console application,
Add SSH.NET Nuget package as below,
Package Manager Console,
Install-Package SSH.NET -Version 2020.0.2
Note: Please use the latest version.
Here we need to follow the below high-level steps to either download or upload the file below,
- Connect SFTP server
- Calculate the file size to be uploaded.
- Initialize Progress variable
- Define a callback used for tracking the progress bar
Connect to SFTP using SFTPClient
SSH.NET provides SFTPClient class that lets you connect to the SFTP server securely and access files for
read, write, upload, or download purposes,
Below the sample code read the FTP file and write their name on the console.
using (var client = new SftpClient("****", "user", "****"))
{
client.Connect();
}
Adding Progress bar – UploadFile SFTP operations
To implement a progress bar for file transfers using SFTPClient in C# with a callback mechanism, you can define a delegate and pass it as a callback parameter to the UploadFile
.
Define a callback method
Let’s define the progress callback delegate,
Action progressBarCallback = (uploaded) => { uploadedBytes = (long)uploaded; // Calculate the progress percentage progress = (int)((uploadedBytes * 100) / fileSize); // Trackthe progress bar TrackProgress(progress); };
Above Inside the event handlers, we calculate the progress percentage based on the total file size and the size of the uploaded or downloaded bytes.
Then, we call the UpdateProgressBar method to update and display the progress bar in the console.
Tracking Progress – SFTP Upload file
Here is the complete example for Tracking Progress – SFTP Upload file.
// Create an SFTP client
using (var sftpClient = new SftpClient("Host", "UserName", "Passowrd"))
{
// Connect to the SFTP
sftpClient.Connect();
// Calculate the file size
var fileSize = new FileInfo(localDirectoryFilePath).Length;
// Initialize the progress variables
long uploadedBytes = 0;
int progress = 0;
// Define the progress callback delegate
Action<ulong> progressBarCallback = (uploaded) =>
{
uploadedBytes = (long)uploaded;
// Calculate the progress percentage
progress = (int)((uploadedBytes * 100) / fileSize);
// Track the progress bar
TrackProgress(progress);
};
using (var uplfileStream = System.IO.File.OpenRead(localDirectoryFilePath))
{
sftpClient.UploadFile(uplfileStream, destinationPath, true, progressBarCallback );
}
// Disconnect from the SFTP server
sftpClient.Disconnect();
}
}
Tracking Progress – DownLoadFile SFTP operations
Similarly, please see the below example for performing Download file operations.
using (var sftpClient = new SftpClient("Host, "User", "Pwd"))
{
// Connect to the SFTP server
sftpClient.Connect();
// Calculate the file size
var fileSize = sftpClient.GetAttributes(remoteBaseDirectory).Size;
// Initialize the progress variables
long downloadedBytes = 0;
int progress = 0;
// Define the progress callback delegate
Action<ulong> progressCallback = (downloaded) =>
{
downloadedBytes = (long)downloaded;
// Calculate the progress percentage
progress = (int)((downloadedBytes * 100) / fileSize);
// Track the progress bar
TrackProgress(progress);
};
// Download the file with progress reporting
sftpClient.DownloadFile(remoteBaseDirectory, File.OpenWrite(localDirectoryFilePath), progressCallback);
}
Print the progress on the Console
The TrackProgressBar
the method lets you print the progress percentage on Console or UI (if using UI display purposes) using the calculated progress percentage.
static void TrackProgressBar(int progress) { Console.Write("\r[{0}{1}] {2}%", new string('#', progress / 10), new string('-', 10 - (progress / 10)), progress); }
That’s all, as we can see we can leverage a callback parameter to the UploadFile and DownloadFile methods as supported to track the progress of a long-awaited operation.
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.