Skip to content

Directory Transfer

Robinicks edited this page Feb 5, 2020 · 25 revisions

API

  • UploadDirectory() - Uploads the specified directory onto the server. If any rules are provided then we only upload the files and folders matching all the rules. Returns one FtpResult per file or folder, containing its detailed transfer status. All exceptions during uploading are caught, and the exception is stored in the related FtpResult and uploading continues. Optionally verifies the hash of the files & retries transfer if hash mismatches.

    • In Mirror mode, we will upload missing files, and delete any extra files from the server that are not present on disk. This is very useful when publishing an exact copy of a local folder onto an FTP server.

    • In Update mode, we will only upload missing files and preserve any extra files on the server. This is useful when you want to simply upload missing files to a server.

  • DownloadDirectory() - Downloads the specified directory onto the local file system. If any rules are provided then we only download the files and folders matching all the rules. Returns one FtpResult per file or folder, containing its detailed transfer status. All exceptions during downloading are caught, and the exception is stored in the related FtpResult and downloading continues. Optionally verifies the hash of a file & retries transfer if hash mismatches.

    • In Mirror mode, we will download missing files, and delete any extra files from disk that are not present on the server. This is very useful when creating an exact local backup of an FTP directory.

    • In Update mode, we will only download missing files and preserve any extra files on disk. This is useful when you want to simply download missing files from an FTP directory.

What is the difference between the Mirror and Update modes?

Mirror mode is a bit more dangerous because files are deleted from the target if they are not present in the source. This is very useful when you want to create an exact backup, for example, and when you want the backup to contain only the files that are in the source and nothing more.

Update mode is safer and simply transfers the missing files (or overwrites all the files if FtpRemoteExists.Overwrite is used). And Update mode does not delete anything from the target.

How do I tell if some files failed to transfer?

You can check the list of FtpResult objects returned by the method. You can filter all the objects where result.IsSuccess = false and then you can read the result.Exception property to get the exact error that caused it to fail.

  • If any files already existed in the target and they were skipped, then the result object will have result.IsSkipped = true and result.IsSuccess = true.

  • If any files did not pass the rules provided and thus were skipped for copying, then the result object will have result.IsSkipped = true and result.IsSkippedByRule = true and result.IsSuccess = true.

  • The files that were actually copied or overwritten will have result.IsSkipped = false and result.IsSuccess = true.

What kinds of rules are supported and how do rules work?

Rules allow you to declaratively specify which files and folders should be copied and which should be skipped. There are rules that let you filter on file extension (eg: upload only PDF files) or file size (eg: upload files smaller than 100 MB) and various other parameters.

Only files that pass all the rules are copied from the source to the target. If you are using Mirror mode then the files that fail the rules will be also deleted from the target.

The rules that are supported are listed here:

Rule Class Parameters Description
FtpFileExtensionRule extensions Only transfer files of a certain extension, or blacklist files of a certain extension.
FtpFileNameRule names Only transfer files of a certain name, or blacklist files of a certain name
FtpFolderNameRule names Only transfer folders of a certain name, or blacklist folders of a certain name
FtpSizeRule ruleOperator, x, y Only transfer files within a certain size, or blacklist files above/below a certain size

How do I use rules to only download PDF files under 1 GB?

Declare the rules and then call DownloadDirectory or UploadDirectory with those rules:

var rules = new List<FtpRule>{
   new FtpFileExtensionRule(true, new List<string>{ "pdf" }),  // only allow PDF files
   new FtpSizeRule(FtpOperator.LessThan, 1000000000)           // only allow files <1 GB
};

client.DownloadDirectory(@"C:\website\attachments\", @"/public_html/attachments",
    FtpFolderSyncMode.Update, FtpLocalExists.Skip, FtpVerify.None, rules);
Clone this wiki locally