FTP provides a way to transfer files, which is fast, but there are no standard built-in ways to ensure that the data got transferred intact.
Although we use .NET socket to transfer data and the underlying TCP/IP ensures that all data packets are transferred properly, mishandling of buggy firewall or third-party application may happen, leading the FTP client to think that the transfer was successful when the transferred data has been compromised and incomplete. This is why we need a checksum mechanism. This article describes how to check the file checksum after upload to FTP server using the Ultimate FTP. You can use CRC, MD5, and SHA1 algorithms for checksum verification.
First, your remote FTP server must support the checksum calculation. Not all FTP servers support this; even the RFC specification does not define a command to calculate a checksum of a remote file. Many modern FTP servers support determining checksums (CRC-32, MD5 or SHA-1) of remote files. And they use different commands like XSHA1
, XSHA256
, XSHA512
, MD5
, XMD5
, XCRC
, and HASH
.
We handle these command automatically. You can use Ftp object's GetSupportedChecksumTypes
method to determine whether the FTP server supports this functionality (and which algorithms). If it does, use GetFileChecksum
method to get a checksum of a file at the server. The corresponding checksums of local files can be calculated using CalculateLocalChecksum()
method of DiskFileSystem
.
This way, we can ensure that the files are uploaded correctly by comparing the server checksum with the local files.
We now take advantage of those methods in the following example:
You will need to add namespaces ComponentPro.IO and ComponentPro.Net.
using System;
using System.Collections.Generic;
using System.Text;
using ComponentPro.IO;
using ComponentPro.Net;
Then we create a new instance of the Ftp
class, connect to the FTP server and then authenticate the user. In this example, we use 'myserver.' That should be changed to your server's IP address or hostname. The credentials should also be changed to yours.
// Create a new class instance.
Ftp client = new Ftp();
// Connect to the FTP server.
client.Connect("myserver");
// Or you can specify the FTP port with
// client.Connect("myserver", 21);
// Authenticate.
client.Authenticate("userName", "password");
Now we want to know if the server supports any checksum calculation methods.
FileChecksumType[] supportedChecksumTypes = client.GetSupportedChecksumTypes();
if (supportedChecksumTypes.Length == 0)
{
// Show error and exit.
Console.WriteLine("No checksum algorithm supported by the server.");
client.Disconnect();
return;
}
Yea, it supports at least one method. We can now upload a file to the server.
// Upload the file.
const string remoteFile = "/my remote file.dat";
const string localFile = "my local file";
long transferred = client.UploadFile(localFile, remoteFile);
Console.WriteLine("Bytes transferred: " + transferred);
After uploading we get the remote file's checksum.
// Get remote checksum
// Suppose the server support at least one algorithm.
string remoteChecksum = client.GetFileChecksum(supportedChecksumTypes[0], remoteFile);
Console.WriteLine("Remote checksum: " + remoteChecksum);
Then we calculate the local file checksum and compare them. The library DiskFileSystem
class in ComponentPro.FileSystem.dll already has that method named CalculateLocalChecksum
.
// Get local checksum
string localChecksum = DiskFileSystem.CalculateLocalChecksum(supportedChecksumTypes[0], localFile);
Console.WriteLine("Local checksum: " + localChecksum);
if (localChecksum == remoteChecksum)
{
Console.WriteLine("File uploaded successfully. {0} checked.", supportedChecksumTypes[0]);
}
else
{
Console.WriteLine("File uploaded successfully. {0} not matched.", supportedChecksumTypes[0]);
}
We close the connection when done.
// Disconnect.
client.Disconnect();