Scenario: Implement FTP solution using FluentFTP library
Solution:
Per fluentFTP documentation
FluentFTP is a fully managed FTP and FTPS library for .NET & .NET Standard, optimized for speed. It provides extensive FTP commands, File uploads/downloads, SSL/TLS connections, Automatic directory listing parsing, File hashing/checksums, File permissions/CHMOD, FTP proxies, FXP transfers, UTF-8 support, Async/await support, Powershell support and more.
- Add latest FluentFTP nuget package to the project
- Create FluentFtp class to download/upload to FTP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.IO;
using System.Net;
using FluentFTP;
namespace TestApp
{
public class FluentFTPUtils
{
public void Download(Context context)
{
var ftpClient = new FtpClient
{
Host = _host,
Credentials = new NetworkCredential { UserName = "{user}", Password = "{pass}" },
Port = _port,
DataConnectionType = _connectionType,
DownloadDataType = FtpDataType.Binary,
UploadDataType = FtpDataType.Binary,
ConnectTimeout = _timeout,
ReadTimeout = _rimeout,
DataConnectionConnectTimeout = _dcTimeout,
DataConnectionReadTimeout = _drTimeout
}; ;
ftpClient.Connect();
ftpClient.SetWorkingDirectory(_workingDirectory);
foreach (
var file in
ftpClient.GetListing(ftpClient.GetWorkingDirectory())
.Where(f => f.Type == FtpFileSystemObjectType.File && f.Name == "fileName")
.OrderBy(f => f.Modified))
{
Stream destStream = null;
try
{
ftpClient.Download(destStream, file.Name);
}
finally
{
destStream?.Close();
}
if (delete)
{
ftpClient.DeleteFile(file.Name);
}
else if (rename)
{
ftpClient.MoveFile(file.Name, newFilename);
}
}
CloseFtpConnection(ftpClient);
}
public void Upload(Context context)
{
var ftpClient = new FtpClient
{
Host = _host,
Credentials = new NetworkCredential { UserName = "{user}", Password = "{pass}" },
Port = _port,
DataConnectionType = _connectionType,
DownloadDataType = FtpDataType.Binary,
UploadDataType = FtpDataType.Binary,
ConnectTimeout = _timeout,
ReadTimeout = _rimeout,
DataConnectionConnectTimeout = _dcTimeout,
DataConnectionReadTimeout = _drTimeout
}; ;
ftpClient.Connect();
Stream inputStream = null;
try
{
inputStream = document.CreateStream();
ftpClient.Upload(inputStream, "fileName");
}
finally
{
inputStream?.Close();
}
CloseFtpConnection(ftpClient);
}
private void CloseFtpConnection(FtpClient ftpConnection)
{
if (ftpConnection != null)
{
ftpConnection.Dispose();
}
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | using System; using System.IO; using System.Net; using FluentFTP; namespace TestApp { public class FluentFTPUtils { public void Download(Context context) { var ftpClient = new FtpClient { Host = _host, Credentials = new NetworkCredential { UserName = "{user}", Password = "{pass}" }, Port = _port, DataConnectionType = _connectionType, DownloadDataType = FtpDataType.Binary, UploadDataType = FtpDataType.Binary, ConnectTimeout = _timeout, ReadTimeout = _rimeout, DataConnectionConnectTimeout = _dcTimeout, DataConnectionReadTimeout = _drTimeout }; ; ftpClient.Connect(); ftpClient.SetWorkingDirectory(_workingDirectory); foreach ( var file in ftpClient.GetListing(ftpClient.GetWorkingDirectory()) .Where(f => f.Type == FtpFileSystemObjectType.File && f.Name == "fileName") .OrderBy(f => f.Modified)) { Stream destStream = null; try { ftpClient.Download(destStream, file.Name); } finally { destStream?.Close(); } if (delete) { ftpClient.DeleteFile(file.Name); } else if (rename) { ftpClient.MoveFile(file.Name, newFilename); } } CloseFtpConnection(ftpClient); } public void Upload(Context context) { var ftpClient = new FtpClient { Host = _host, Credentials = new NetworkCredential { UserName = "{user}", Password = "{pass}" }, Port = _port, DataConnectionType = _connectionType, DownloadDataType = FtpDataType.Binary, UploadDataType = FtpDataType.Binary, ConnectTimeout = _timeout, ReadTimeout = _rimeout, DataConnectionConnectTimeout = _dcTimeout, DataConnectionReadTimeout = _drTimeout }; ; ftpClient.Connect(); Stream inputStream = null; try { inputStream = document.CreateStream(); ftpClient.Upload(inputStream, "fileName"); } finally { inputStream?.Close(); } CloseFtpConnection(ftpClient); } private void CloseFtpConnection(FtpClient ftpConnection) { if (ftpConnection != null) { ftpConnection.Dispose(); } } } } |
No comments:
Post a Comment