Download
Overview
DownloadTask downloads files from remote FTP or SFTP servers to local directories. It supports parallel downloading, file archiving on the remote server, deletion after transfer, and checked transfers to ensure files are not actively being written before download.
Use this task when: You need to retrieve data files from external partners, vendors, or remote servers via FTP/SFTP protocols.
Don't use this task when:
- Downloading files via HTTP/HTTPS (use a web download client instead)
- Accessing cloud storage like S3 or Azure Blob Storage (use cloud-specific clients)
- Files are already on the local filesystem (use file copy operations instead)
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
Host |
string |
Yes | - | The FTP/SFTP server hostname or IP address |
Port |
int |
No | 22 |
The server port (22 for SFTP, 21 for FTP) |
Username |
string |
Yes | - | Authentication username for the server |
Password |
string |
Yes | - | Authentication password for the server |
Protocol |
ProtocolSubset |
No | Sftp |
Transfer protocol: Sftp or Ftp |
ConnectionTimeoutInSeconds |
int |
No | 120 |
Maximum time to wait for server connection |
RemoteDirectory |
string |
No | "/" |
Directory on remote server to search for files |
RemoteFileName |
string |
No | null |
Specific filename or pattern to download (e.g., "*.csv") |
LocalDirectory |
string |
Yes | - | Local directory where files will be saved |
RemoteArchiveDirectory |
string |
No | null |
If set, files are moved to this remote directory after successful download |
MaxDownloaders |
int |
No | 10 |
Maximum number of concurrent download threads |
DeleteOnSuccess |
bool |
No | false |
Delete files from remote server after successful download |
UseCheckedTransfer |
bool |
No | false |
Wait for files to stop changing before downloading (prevents partial transfers) |
Execution Flow
Processing Algorithm
The download process uses a multi-threaded approach to efficiently transfer multiple files:
- File Discovery: Connects to the remote server and searches the
RemoteDirectoryfor files matchingRemoteFileName(supports wildcards) - Queue Creation: All matching files are added to a download queue
- Concurrent Download: Up to
MaxDownloadersthreads run in parallel, each pulling files from the queue - Checked Transfer (optional): If
UseCheckedTransfer = true, each file is checked multiple times to ensure its size remains constant (indicating the write is complete) before downloading - Post-Transfer Actions: After successful download, files can be deleted or moved to an archive directory on the remote server
- Result Tracking: The task tracks succeeded and failed transfers, logging detailed results
Example Execution
Scenario: Downloading 100 CSV files from a vendor SFTP server with 10 concurrent downloaders:
Configuration:
- MaxDownloaders = 10
- RemoteFileName = "*.csv"
- RemoteDirectory = "/outbound/claims"
- UseCheckedTransfer = true
Execution:
1. Connect to SFTP server
2. Find 100 files matching "*.csv" in "/outbound/claims"
3. Add all 100 files to download queue
4. Start 10 downloader threads
5. Each thread:
- Pull next file from queue
- Check file size (wait if still changing)
- Download file to LocalDirectory
- Move file to RemoteArchiveDirectory
- Process next file from queue
6. Complete when queue is empty
7. Log: "Completed. 100 Succeeded. 0 Failed."
Timeline: With 3KB files and stable network, approximately 10-30 seconds total
Usage Examples
Example 1: Basic SFTP download with archiving
// Download all CSV files from vendor and archive them on remote server
var task = new DownloadTask
{
Host = "vendor.sftp.example.com",
Port = 22,
Username = "mdclarity_user",
Password = "secure_password",
Protocol = ProtocolSubset.Sftp,
RemoteDirectory = "/outbound/data",
RemoteFileName = "*.csv",
LocalDirectory = @"C:\MDClarity\Downloads\Vendor",
RemoteArchiveDirectory = "/archive/processed",
// Files will be moved to /archive/processed after download
};
Example 2: Download specific file and delete after success
var task = new DownloadTask
{
Host = "partner.ftp.example.com",
Port = 21,
Username = "integration_user",
Password = "password123",
Protocol = ProtocolSubset.Ftp,
RemoteDirectory = "/reports",
RemoteFileName = "daily_claims_[id].txt", // Specific filename
LocalDirectory = @"C:\MDClarity\Reports",
DeleteOnSuccess = true, // Remove from remote server after download
ConnectionTimeoutInSeconds = 60
};
Example 3: High-volume download with checked transfer
// Download many files with parallel transfers, ensuring files are complete before downloading
var task = new DownloadTask
{
Host = "data.sftp.example.com",
Username = "mdclarity_prod",
Password = "prod_password",
Protocol = ProtocolSubset.Sftp,
RemoteDirectory = "/exports/visits",
RemoteFileName = "visit_*.dat", // Pattern matching multiple files
LocalDirectory = @"C:\MDClarity\Imports\Visits",
MaxDownloaders = 15, // Allow up to 15 concurrent downloads
UseCheckedTransfer = true, // Wait for files to finish writing before download
RemoteArchiveDirectory = "/exports/archive"
};
Example 4: Download all files from a directory
var task = new DownloadTask
{
Host = "backup.sftp.internal",
Username = "backup_user",
Password = "backup_pass",
Protocol = ProtocolSubset.Sftp,
RemoteDirectory = "/daily_exports",
RemoteFileName = null, // null or omitted = download all files in directory
LocalDirectory = @"C:\MDClarity\Backups\Daily",
MaxDownloaders = 5 // Conservative for potentially large files
};
Related Tasks Comparison
| Task | Use Case | Direction | Protocol | Post-Transfer Actions |
|---|---|---|---|---|
| Download | Retrieve files from remote servers | Remote → Local | FTP/SFTP | Remote archive or delete |
Upload |
Send files to remote servers | Local → Remote | FTP/SFTP | Local archive or delete |
LoadDataFile |
Import downloaded files into database | Local file → Database | N/A (local files) | File archiving |
AddFilesToQueue |
Queue files for processing | Directory scan → Database queue | Local or S3 | None (creates queue entries) |
Key Differences:
- Download vs Upload: Download retrieves files from remote to local; Upload sends files from local to remote
- Download vs LoadDataFile: Download retrieves files; LoadDataFile imports them into database tables (often used sequentially)
- Download vs AddFilesToQueue: Download transfers files; AddFilesToQueue creates processing queue entries for existing files
Performance Considerations
Network Impact
- Bandwidth: Downloads are limited by network bandwidth and server capacity
- Concurrent Connections:
MaxDownloaderscontrols parallelism (more threads = faster for many small files, but may overwhelm network or remote server) - Connection Timeout:
ConnectionTimeoutInSecondsshould be increased for slow or unreliable connections - Checked Transfer:
UseCheckedTransfer = trueadds latency (rechecks every 10ms up to 3 times) but prevents downloading incomplete files
Optimization Tips
| Scenario | Recommendation | Rationale |
|---|---|---|
| Many small files (100s of 3KB files) | MaxDownloaders = 10-15 |
Parallel downloads maximize throughput for small files |
| Few large files (GBs) | MaxDownloaders = 1-3 |
Avoid saturating bandwidth; focus on stable transfers |
| Unreliable network | ConnectionTimeoutInSeconds = 300, UseCheckedTransfer = true |
Allow more time for connections; ensure complete files |
| Active file writes on remote | UseCheckedTransfer = true |
Prevents downloading files that are still being written |
| High-volume daily imports | Use RemoteArchiveDirectory instead of DeleteOnSuccess |
Preserves audit trail; allows reprocessing if needed |