翼度科技»论坛 编程开发 .net 查看内容

C# 利用FluentFTP实现FTP上传下载功能

2

主题

2

帖子

6

积分

新手上路

Rank: 1

积分
6
FTP作为日常工作学习中,非常重要的一个文件传输存储空间,想必大家都非常的熟悉了,那么如何快速的实现文件的上传下载功能呢,本文以一个简单的小例子,简述如何通过FluentFTP实现文件的上传和下载功能。仅供学习分享使用,如有不足之处,还请指正。
FTP基础知识

文件传输协议(File Transfer Protocol,FTP)是用于在网络上进行文件传输的一套标准协议,它工作在 OSI 模型的第七层, TCP 模型的第四层, 即应用层, 使用 TCP 传输而不是 UDP, 客户在和服务器建立连接前要经过一个“三次握手”的过程, 保证客户与服务器之间的连接是可靠的, 而且是面向连接, 为数据传输提供可靠保证。FTP允许用户以文件操作的方式(如文件的增、删、改、查、传送等)与另一主机相互通信。然而, 用户并不真正登录到自己想要存取的计算机上面而成为完全用户, 可用FTP程序访问远程资源, 实现用户往返传输文件、目录管理以及访问电子邮件等等, 即使双方计算机可能配有不同的操作系统和文件存储方式。
FTP环境搭建

在windows操作系统中,FTP可以通过(Internet Inforamtion Services, IIS)管理器进行创建,创建成功后即可进行查看,如下所示:

 
FluentFTP安装

FluentFTP是一款基于.Net的FTP和FTPS的客户端动态库,操作简单便捷。

 
 
首先创建基于.Net Framework 4.6.1的winform应用程序,然后通过Nuget包管理器进行安装,如下所示:

 
示例演示

 主要实现基于FTP的上传,下载,浏览等功能,如下所示:

 
 进入文件夹及右键下载,如下所示:

 
示例源码

FtpHelper类源码,封装了FTP文件的检索,上传,下载等功能,如下所示:
  1.   1 using System;
  2.   2 using System.Collections.Generic;
  3.   3 using System.IO;
  4.   4 using System.Linq;
  5.   5 using System.Net;
  6.   6 using System.Text;
  7.   7 using System.Threading;
  8.   8 using System.Threading.Tasks;
  9.   9 using FluentFTP;
  10. 10
  11. 11 namespace DemoFtp
  12. 12 {
  13. 13     public class FtpHelper
  14. 14     {
  15. 15         #region 属性与构造函数
  16. 16
  17. 17         /// <summary>
  18. 18         /// IP地址
  19. 19         /// </summary>
  20. 20         public string IpAddr { get; set; }
  21. 21
  22. 22         /// <summary>
  23. 23         /// 相对路径
  24. 24         /// </summary>
  25. 25         public string RelatePath { get; set; }
  26. 26
  27. 27         /// <summary>
  28. 28         /// 端口号
  29. 29         /// </summary>
  30. 30         public int Port { get; set; }
  31. 31
  32. 32         /// <summary>
  33. 33         /// 用户名
  34. 34         /// </summary>
  35. 35         public string UserName { get; set; }
  36. 36
  37. 37         /// <summary>
  38. 38         /// 密码
  39. 39         /// </summary>
  40. 40         public string Password { get; set; }
  41. 41
  42. 42         public FtpHelper()
  43. 43         {
  44. 44
  45. 45         }
  46. 46
  47. 47         public FtpHelper(string ipAddr, int port, string userName, string password, string relatePath)
  48. 48         {
  49. 49             this.IpAddr = ipAddr;
  50. 50             this.Port = port;
  51. 51             this.UserName = userName;
  52. 52             this.Password = password;
  53. 53             this.RelatePath = relatePath;
  54. 54         }
  55. 55
  56. 56         #endregion
  57. 57
  58. 58         #region 方法
  59. 59
  60. 60         public FtpListItem[] ListDir() {
  61. 61             FtpListItem[] lists;
  62. 62             using (var ftpClient = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
  63. 63             {
  64. 64                 ftpClient.Connect();
  65. 65                 ftpClient.SetWorkingDirectory(this.RelatePath);
  66. 66                 lists = ftpClient.GetListing();
  67. 67             }
  68. 68             return lists;
  69. 69         }
  70. 70
  71. 71         public void UpLoad(string dir, string file, out bool isOk)
  72. 72         {
  73. 73             isOk = false;
  74. 74             FileInfo fi = new FileInfo(file);
  75. 75             using (FileStream fs = fi.OpenRead())
  76. 76             {
  77. 77                 //long length = fs.Length;
  78. 78                 using (var ftpClient = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
  79. 79                 {
  80. 80                     ftpClient.Connect();
  81. 81                     ftpClient.SetWorkingDirectory(this.RelatePath);
  82. 82                     string remotePath = dir + "/" + Path.GetFileName(file);
  83. 83                     var ftpRemodeExistsMode = file.EndsWith(".txt") ? FtpRemoteExists.Overwrite : FtpRemoteExists.Skip;
  84. 84                     FtpStatus status = ftpClient.UploadStream(fs, remotePath, ftpRemodeExistsMode, true);
  85. 85                     isOk = status == FtpStatus.Success;
  86. 86
  87. 87                 }
  88. 88             }
  89. 89         
  90. 90         }
  91. 91
  92. 92         /// <summary>
  93. 93         /// 上传多个文件
  94. 94         /// </summary>
  95. 95         /// <param name="files"></param>
  96. 96         /// <param name="isOk"></param>
  97. 97         public void UpLoad(string dir, string[] files, out bool isOk)
  98. 98         {
  99. 99             isOk = false;
  100. 100             if (CheckDirIsExists(dir))
  101. 101             {
  102. 102                 foreach (var file in files)
  103. 103                 {
  104. 104                     UpLoad(dir, file, out isOk);
  105. 105                 }
  106. 106             }
  107. 107         }
  108. 108
  109. 109
  110. 110         private bool CheckDirIsExists(string dir)
  111. 111         {
  112. 112             bool flag = false;
  113. 113             using (var ftpClient = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
  114. 114             {
  115. 115                 ftpClient.Connect();
  116. 116                 ftpClient.SetWorkingDirectory(this.RelatePath);
  117. 117                 flag = ftpClient.DirectoryExists(dir);
  118. 118                 if (!flag)
  119. 119                 {
  120. 120                     flag = ftpClient.CreateDirectory(dir);
  121. 121                 }
  122. 122             }
  123. 123             return flag;
  124. 124
  125. 125
  126. 126         }
  127. 127
  128. 128         /// <summary>
  129. 129         /// 下载ftp
  130. 130         /// </summary>
  131. 131         /// <param name="localAddress"></param>
  132. 132         /// <param name="remoteAddress"></param>
  133. 133         /// <returns></returns>
  134. 134         public bool DownloadFile(string localAddress, string remoteAddress)
  135. 135         {
  136. 136             using (var ftpClient = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
  137. 137             {
  138. 138                 ftpClient.SetWorkingDirectory("/");
  139. 139                 ftpClient.Connect();
  140. 140                 if (ftpClient.DownloadFile(localAddress, remoteAddress) == FtpStatus.Success)
  141. 141                 {
  142. 142                     return true;
  143. 143                 }
  144. 144                 return false;
  145. 145             }
  146. 146         }
  147. 147
  148. 148         #endregion
  149. 149     }
  150. 150 }
复制代码
每一个FTP文件或文件夹,由一个自定义用户控件【PictureBox+Label+ContextMenu】表示,这样便于处理与显示:
  1. 1 using DemoFtp.Properties;
  2. 2 using FluentFTP;
  3. 3 using System;
  4. 4 using System.Collections.Generic;
  5. 5 using System.ComponentModel;
  6. 6 using System.Data;
  7. 7 using System.Drawing;
  8. 8 using System.IO;
  9. 9 using System.Linq;
  10. 10 using System.Text;
  11. 11 using System.Threading.Tasks;
  12. 12 using System.Windows.Forms;
  13. 13
  14. 14 namespace DemoFtp
  15. 15 {
  16. 16     public partial class FtpElementControl : UserControl
  17. 17     {
  18. 18         public Action<FtpListItem> SubFolderClick;
  19. 19
  20. 20         public Action<FtpListItem> DownLoadClick;
  21. 21
  22. 22         private FtpListItem ftpListItem;
  23. 23
  24. 24         public FtpElementControl(FtpListItem ftpListItem)
  25. 25         {
  26. 26             InitializeComponent();
  27. 27             this.ftpListItem = ftpListItem;
  28. 28         }
  29. 29
  30. 30         public FtpElementControl()
  31. 31         {
  32. 32             InitializeComponent();
  33. 33         }
  34. 34
  35. 35         public void InitControl()
  36. 36         {
  37. 37             if (ftpListItem.Type == FtpObjectType.Directory)
  38. 38             {
  39. 39                 this.pbIcon.Image = Resources.folder.ToBitmap();
  40. 40             }
  41. 41             else if (ftpListItem.Type == FtpObjectType.File)
  42. 42             {
  43. 43                 var name = ftpListItem.Name;
  44. 44                 var ext = Path.GetExtension(name).ToLower().Substring(1);
  45. 45                 if (ext == "png" || ext == "jpeg" || ext == "jpg" || ext == "bmp" || ext == "gif")
  46. 46                 {
  47. 47                     this.pbIcon.Image = Resources.pictures.ToBitmap();
  48. 48                 }
  49. 49                 else if (ext == "doc" || ext == "docx")
  50. 50                 {
  51. 51                     this.pbIcon.Image = Resources.doc.ToBitmap();
  52. 52                 }
  53. 53                 else if (ext == "exe")
  54. 54                 {
  55. 55                     this.pbIcon.Image = Resources.setup.ToBitmap();
  56. 56                 }
  57. 57                 else
  58. 58                 {
  59. 59                     this.pbIcon.Image = Resources.file;
  60. 60                 }
  61. 61             }
  62. 62             else
  63. 63             {
  64. 64                 this.pbIcon.Image = Resources.file;
  65. 65             }
  66. 66             this.lblName.Text = ftpListItem.Name;
  67. 67         }
  68. 68
  69. 69         private void FtpElementControl_Load(object sender, EventArgs e)
  70. 70         {
  71. 71            
  72. 72         }
  73. 73
  74. 74         /// <summary>
  75. 75         /// 子菜单下载功能
  76. 76         /// </summary>
  77. 77         /// <param name="sender"></param>
  78. 78         /// <param name="e"></param>
  79. 79         private void menu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  80. 80         {
  81. 81             this.DownLoadClick?.Invoke(ftpListItem);
  82. 82         }
  83. 83
  84. 84         /// <summary>
  85. 85         /// 双击打开
  86. 86         /// </summary>
  87. 87         /// <param name="sender"></param>
  88. 88         /// <param name="e"></param>
  89. 89         private void pbIcon_DoubleClick(object sender, EventArgs e)
  90. 90         {
  91. 91             this.SubFolderClick?.Invoke(ftpListItem);
  92. 92         }
  93. 93     }
  94. 94 }
复制代码
主页面由一系列用户操作框和按钮组成,完成对FTP的基本操作,如下所示:
  1.   1 using FluentFTP;
  2.   2 using System;
  3.   3 using System.Collections.Generic;
  4.   4 using System.ComponentModel;
  5.   5 using System.Data;
  6.   6 using System.Drawing;
  7.   7 using System.IO;
  8.   8 using System.Linq;
  9.   9 using System.Text;
  10. 10 using System.Threading.Tasks;
  11. 11 using System.Windows.Forms;
  12. 12
  13. 13 namespace DemoFtp
  14. 14 {
  15. 15     public partial class MainForm : Form
  16. 16     {
  17. 17         private FtpHelper ftpHelper;
  18. 18
  19. 19         public MainForm()
  20. 20         {
  21. 21             InitializeComponent();
  22. 22         }
  23. 23
  24. 24         private void btnLogin_Click(object sender, EventArgs e)
  25. 25         {
  26. 26             var url = txtFtpUrl.Text;
  27. 27             var userName = txtUserName.Text;
  28. 28             var password = txtPassword.Text;
  29. 29             var port = txtPort.Text;
  30. 30             if (this.lblRelatePath.Text != "/")
  31. 31             {
  32. 32                 this.lblRelatePath.Text = "/";
  33. 33             }
  34. 34             var relatePath = this.lblRelatePath.Text;
  35. 35             if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(port))
  36. 36             {
  37. 37                 MessageBox.Show("路径和账号密码不可为空");
  38. 38                 return;
  39. 39             }
  40. 40             if (ftpHelper == null)
  41. 41             {
  42. 42                 ftpHelper = new FtpHelper(url, int.Parse(port), userName, password, relatePath);
  43. 43
  44. 44             }
  45. 45             ListDir();
  46. 46         }
  47. 47
  48. 48         public void SubFolder(FtpListItem ftpListItem)
  49. 49         {
  50. 50             if (ftpListItem.Type == FtpObjectType.Directory)
  51. 51             {
  52. 52                 var fullName = ftpListItem.FullName;
  53. 53                 ftpHelper.RelatePath = fullName;
  54. 54                 ListDir();
  55. 55                 this.lblRelatePath.Text = fullName;
  56. 56             }
  57. 57         }
  58. 58
  59. 59
  60. 60         private void Download(FtpListItem ftpListItem) {
  61. 61             var fullName=ftpListItem.FullName;
  62. 62             var fileName = Path.GetFileName(fullName);
  63. 63             SaveFileDialog sfd = new SaveFileDialog();
  64. 64             sfd.FileName = fileName;
  65. 65             sfd.Title = "不载";
  66. 66             sfd.Filter = "所有文档|*.*";
  67. 67             if (DialogResult.OK == sfd.ShowDialog()) {
  68. 68                 ftpHelper.DownloadFile(sfd.FileName, fullName);
  69. 69             }
  70. 70         }
  71. 71
  72. 72         private void ListDir()
  73. 73         {
  74. 74             this.ftpContainer.Controls.Clear();
  75. 75             var ftpListItems = this.ftpHelper.ListDir();
  76. 76             if (ftpListItems != null && ftpListItems.Length > 0)
  77. 77             {
  78. 78                 foreach (var ftpListItem in ftpListItems)
  79. 79                 {
  80. 80                     FtpElementControl ftpControl = new FtpElementControl(ftpListItem);
  81. 81                     ftpControl.InitControl();
  82. 82                     ftpControl.DownLoadClick += Download;
  83. 83                     ftpControl.SubFolderClick += SubFolder;
  84. 84                     this.ftpContainer.Controls.Add(ftpControl);
  85. 85                 }
  86. 86             }
  87. 87         }
  88. 88
  89. 89         private void btnUpload_Click(object sender, EventArgs e)
  90. 90         {
  91. 91             OpenFileDialog ofd = new OpenFileDialog();
  92. 92             ofd.Filter = "所有文件|*.*";
  93. 93             ofd.Title = "请选择需要上传的文件";
  94. 94             if (DialogResult.OK == ofd.ShowDialog()) {
  95. 95                 var localFile=ofd.FileName;
  96. 96                 ftpHelper.UpLoad(this.lblRelatePath.Text, localFile, out bool isOk);
  97. 97                 if (isOk) {
  98. 98                     ListDir();
  99. 99                 }
  100. 100             }
  101. 101         }
  102. 102
  103. 103         private void pbReturn_Click(object sender, EventArgs e)
  104. 104         {
  105. 105             var relativePath=this.lblRelatePath.Text;
  106. 106             if (relativePath == "/") {
  107. 107                 return;
  108. 108             }
  109. 109             relativePath = relativePath.Substring(0, relativePath.LastIndexOf("/")+1);
  110. 110             ftpHelper.RelatePath=relativePath;
  111. 111             ListDir();
  112. 112             this.lblRelatePath.Text = relativePath;
  113. 113         }
  114. 114     }
  115. 115 }
复制代码
以上就是基于FluentFTP实现FTP上传下载的全部内容,旨在抛砖引玉,共同学习,一起进步。

来源:https://www.cnblogs.com/hsiang/archive/2023/02/21/17139285.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具