Delphi IdFTP[1] 使用介绍
Delphi IdFTP[1] 使用介绍
1、属性:
- Host: //FTP服务器地址。
- USER://用户帐号。
- Password: //密码。
- Passive: //用于控制FTP数据连接方法。FTP的数据传输可以有主动和被动。Port: //FTP服务的默认端口是21。
- PORT是主动模式,在建立数据通道时,服务端去连接别人;
- PASV是被动模式,在建立数据通道时,服务端被别人连接;
- 建立数据通道时,用PORT模式还是PASV模式,选择权在于FTP客户端。
- IDFTP里的passive是一个Boolean数据类型。当True时为主动;当false时为被动。
- TransferType: //传输文件的类型,可以取值为FtBinary(8位的任意文件)和FtAscii(7位的文本文件)。
- SystemDesc: //FTP服务器的描述。
2、常用方法
- Connect: //连接FTP服务器.
- Abort: //停止FTP服务的操作。
- Quit: //关闭客户端与服务器的连接.
- Noop: //用于保持连接的命令,防止被服务器关掉连接.
- List: //获取FTP服务器上的文件和目录列表.
- 原型为: procedure List(ADest:TStrings;Const ASpecifier:String;const ADetails:Boolean);
- RetrieveCurrentDir: //取得当前目录的名称.
- Site: //发送FTP服务器的命令.
- 原型为:procedure Site(const ACommand:string);
- ChangeDir: //改变FTP服务器上的当前目录ChangeDirUp: //将FTP服务器上的目录返回到你目录.
- 原型为: procedure ChangeDir(const ADirName:string);
- MakeDir: //在服务器上建立一个新目录.
- 原型为:procedure MakeDir(const ADirName:String);
- RemoveDir: //删除服务器上的一个目录.Get: //从FTP服务器上下载文件.这个方法被重载了. // 所谓“重载”,就是当从多个“类”身上继承一些方法时,会发生冲突。为了避免这一个冲突,就使用了“重载”功能
- 原型为:procedure RemoveDir(const ADirName:string);
- Get: //从FTP服务器上下载文件.这个方法被重载了.
- 原型为:procedure Get(const ASourceFile:string;ADest:TStream);overload;
- procedure Get(const ASourceFile:string;const ADest:string;ACanOverWrite:Boolean);overload;
- Put: //用于向服务器上传文件.该方法被重载.KillDataChannal: //关闭FTP数据通道.
- 原型为:procedure Put(const ASourceFile:TStream;const ADest:string;const Appand:Boolean);overload;
- procedure Put(const ASourceFile:string;const ADest:string;const AAppand:Boolean);overload;
- Delete: //删除FTP服务器上一个文件.
- 原形为:procedure Delete(const AfileName:string);
- Rename: //更改服务器上的文件名.
- 原型为:procedure Rename(const ASourceFile:string;const ADestFile:string);
- Size: //获取文件大小信息.
- 原型为:function Size(const AFileName:string):Integer;
- Delete: //删除FTP服务器上一个文件.
- 原形为:procedure Delete(const AfileName:string);
3、代码示例:
//3.1连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | with idftp do begin try Host := trim(edit1 . Text); //FTP服务器IP地址 Username := trim(edit2 . Text); //用户名 Password := trim(edit3 . Text); //密码 Connect; // 连接 DirectoryListBox . Items . Clear; // 清空目录以及文件信息 DebugListBox . Items . Clear; // 清空记录信息 // SaveFTPHostInfo(trim(CURDIR.Text), 'FTPHOST'); finally if Connected then begin DisplayDir(trim(CURDIR . Text)); // 改变当前路径 FTPCon . Enabled := false ; // 连接按钮 FTPDisCon . Enabled := True ; // 断开按钮 end ; end ; end ; |
//3.2断开按钮的代码:
1 2 3 4 5 6 7 8 9 10 | try if IdFTP . Connected then // 判断客户端是否连着服务器 IdFTP . Abort; if TransferrignData then // 判断客户端与服务器之间是否有数据传输 IdFTP . Quit; finally IdFTP . Disconnect; // 断开连接 FTPCon . Enabled := true ; FTPDisCon . Enabled := false ; end ; |
//3.3TransferrignData 全局变量定义,连接按钮DisplayDir过程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | procedure TFFTPClient . DisplayDir(DirName: string ); var LS: TStringList; begin LS := TStringList . Create; try IdFTP . ChangeDir(DirName); IdFTP . TransferType := ftASCII; // 编译不通过时 USES IdFTPCommon CURDIR . Text := IdFTP . RetrieveCurrentDir; DirectoryListBox . Items . Clear; IdFTP . List(LS); // 把IDFTP里的LIST与LS关联起来 DirectoryListBox . Items . Assign(LS); // 把DIRECTORYLISTBOX 与 LS 关联起来 if DirectoryListBox . Items . Count > 0 then if AnsiPos( 'total' , DirectoryListBox . Items[ 0 ]) > 0 then DirectoryListBox . Items . Delete( 0 ); finally LS . Free; end ; end ; |
//3.4 显示目录(把LISTBOX的STYLE的属性改成lbOwnerDrawFixed,再在ONDRAWITEM方法里添 )
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 | procedure TFFTPClient . DirectoryListBoxDrawItem(Control: TWinControl; Index: Integer ; Rect: TRect; State: TOwnerDrawState); var R: TRect; begin //---------------------------------当选择一条记录时 if odSelected in State then begin DirectoryListBox . Canvas . Brush . Color := $00895F0A ; DirectoryListBox . Canvas . Font . Color := clWhite; end else begin DirectoryListBox . Canvas . Brush . Color := clWindow; end ; //---------------------------------显示当前目录里的信息 if Assigned(IdFTP . DirectoryListing) and (IdFTP . DirectoryListing . Count > Index) then begin DirectoryListBox . Canvas . FillRect(Rect); with IdFTP . DirectoryListing . Items[Index] do begin //------------------------// DirectoryListBox . Canvas . TextOut(Rect . Left, Rect . Top, FileName); // 文件名 R := Rect; R . Left := Rect . Left + HeaderControl1 . Sections . Items[ 0 ].Width; //------------------------// R . Right := R . Left + HeaderControl1 . Sections . Items[ 1 ].Width; DirectoryListBox . Canvas . FillRect(R); DirectoryListBox . Canvas . TextOut(R . Left, Rect . Top, IntToStr(Size)); // 文件大小 //------------------------// R . Left := R . Right; R . Right := R . Left + HeaderControl1 . Sections . Items[ 2 ].Width; DirectoryListBox . Canvas . FillRect(R); if ItemType = ditDirectory then // 是文件夹类型 编译不通过时要USES IDFTPLIST begin DirectoryListBox . Canvas . TextOut(R . Left, Rect . Top, '文件夹' ); end else begin DirectoryListBox . Canvas . TextOut(R . Left, Rect . Top, '文件' ); end ; //------------------------// R . Left := R . Right; R . Right := R . Left + HeaderControl1 . Sections . Items[ 3 ].Width; DirectoryListBox . Canvas . FillRect(R); DirectoryListBox . Canvas . TextOut(R . Left, Rect . Top, FormatDateTime( 'yyyy-mm-dd hh:mm' , ModifiedDate)); // 修改时间 //------------------------// R . Left := R . Right; R . Right := R . Left + HeaderControl1 . Sections . Items[ 4 ].Width; DirectoryListBox . Canvas . FillRect(R); DirectoryListBox . Canvas . TextOut(R . Left, Rect . Top, GroupName); // 组名 //------------------------// R . Left := R . Right; R . Right := R . Left + HeaderControl1 . Sections . Items[ 5 ].Width; DirectoryListBox . Canvas . FillRect(R); DirectoryListBox . Canvas . TextOut(R . Left, Rect . Top, OwnerName); // 拥有者名字 //------------------------// R . Left := R . Right; R . Right := R . Left + HeaderControl1 . Sections . Items[ 6 ].Width; DirectoryListBox . Canvas . FillRect(R); DirectoryListBox . Canvas . TextOut(R . Left, Rect . Top, OwnerPermissions + GroupPermissions + UserPermissions); // 权限 end ; end ; end ; |
提示:可以通过IDFTP里的work,status,workbegin,workend来实现进度条
//3.5 上传代码示例:
1 2 3 4 5 6 7 8 9 10 | if IdFTP . Connected then begin if OpenDialog1 . Execute then try IdFTP . TransferType := ftBinary; IdFTP . Put(OpenDialog1 . FileName, ExtractFileName(OpenDialog1 . FileName)); finally DisplayDir(idftp . RetrieveCurrentDir); end ; end ; |
//3.6 下载代码示例:
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 | procedure TFFTPClient . DirectoryListBoxDblClick(Sender: TObject); var FName: string ; begin if not IdFTP . Connected then // 判断FTP是否还连着服务器 exit; FName := IdFTP . DirectoryListing . Items[DirectoryListBox . ItemIndex].FileName; if IdFTP . DirectoryListing . Items[DirectoryListBox . ItemIndex].ItemType = ditDirectory then // 假如双击是文件夹,就改变路径 begin CURDIR . Text := Trim(FName); DisplayDir(CURDIR . Text); end else begin try SaveDialog1 . FileName := Name; // 得到保存的文件名 if SaveDialog1 . Execute then begin IdFTP . TransferType := ftBinary; // 以二进制进行传输 BytesToTransfer := IdFTP . Size(Name); // 得到文件的大小 if FileExists(Name) then begin case MessageDlg( '文件已经存在,是否重新下载?' , mtConfirmation, mbYesNoCancel, 0 ) of mrYes: begin BytesToTransfer := BytesToTransfer - FileSizeByName(Name); // FileSizeByName在IdGlobal IdFTP . Get(Name, SaveDialog1 . FileName, false , true ); end ; mrNo: begin IdFTP . Get(Name, SaveDialog1 . FileName, true ); end ; mrCancel: begin exit; end ; end ; end else begin IdFTP . Get(Name, SaveDialog1 . FileName, false ); end ; end ; finally end ; end ; end ; |
// 3.7 新建目录:
1 2 3 4 5 6 7 8 9 10 11 | var DirName: string ; begin DirName:=InputBox( '输入新目录名称' , '提示信息' , '' ); if DirName <> '' then try IdFTP . MakeDir(DirName); DisplayDir(CURDIR . Text); finally end ; end ; |
创建时间:2020.05.08 更新时间:
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!
分类:
Delphi 控N-IdFTP
标签:
Delphi
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报