Delphi IdFTP[1] 使用介绍
<p><span style="font-size: 16px">Delphi IdFTP 使用介绍</span><br><span style="font-size: 16px"><strong>1、属性:</strong> </span></p><ul>
<li><span style="font-size: 16px">Host: //FTP服务器地址。</span></li>
<li><span style="font-size: 16px">USER://用户帐号。</span></li>
<li><span style="font-size: 16px">Password: //密码。</span></li>
<li><span style="font-size: 16px">Passive: //用于控制FTP数据连接方法。FTP的数据传输可以有主动和被动。Port: //FTP服务的默认端口是21。</span>
<ul>
<li> PORT是主动模式,在建立数据通道时,服务端去连接别人;</li>
<li> PASV是被动模式,在建立数据通道时,服务端被别人连接;
<ul>
<li>建立数据通道时,用PORT模式还是PASV模式,选择权在于FTP客户端。</li>
<li>IDFTP里的passive是一个Boolean数据类型。当True时为主动;当false时为被动。</li>
</ul>
</li>
</ul>
</li>
<li></li>
<li><span style="font-size: 16px">TransferType: //传输文件的类型,可以取值为FtBinary(8位的任意文件)和FtAscii(7位的文本文件)。</span></li>
<li><span style="font-size: 16px">SystemDesc: //FTP服务器的描述。 </span></li>
</ul>
<p><br><span style="font-size: 16px"><strong>2、常用方法</strong></span></p>
<ul>
<li><span style="font-size: 16px">Connect: //连接FTP服务器.</span></li>
<li><span style="font-size: 16px">Abort: //停止FTP服务的操作。</span></li>
<li><span style="font-size: 16px">Quit: //关闭客户端与服务器的连接.</span></li>
<li><span style="font-size: 16px">Noop: //用于保持连接的命令,防止被服务器关掉连接.</span></li>
<li><span style="font-size: 16px">List: //获取FTP服务器上的文件和目录列表.</span>
<ul>
<li><span style="font-size: 16px">原型为: procedure List(ADest:TStrings;Const ASpecifier:String;const ADetails:Boolean);</span></li>
</ul>
</li>
<li><span style="font-size: 16px">RetrieveCurrentDir: //取得当前目录的名称.</span></li>
<li><span style="font-size: 16px">Site: //发送FTP服务器的命令.</span>
<ul>
<li> 原型为:procedure Site(const ACommand:string);</li>
</ul>
</li>
<li><span style="font-size: 16px">ChangeDir: //改变FTP服务器上的当前目录ChangeDirUp: //将FTP服务器上的目录返回到你目录.</span>
<ul>
<li> 原型为: procedure ChangeDir(const ADirName:string);</li>
</ul>
</li>
<li><span style="font-size: 16px">MakeDir: //在服务器上建立一个新目录.</span>
<ul>
<li> 原型为:procedure MakeDir(const ADirName:String);</li>
</ul>
</li>
<li><span style="font-size: 16px">RemoveDir: //删除服务器上的一个目录.Get: //从FTP服务器上下载文件.这个方法被重载了. // 所谓“重载”,就是当从多个“类”身上继承一些方法时,会发生冲突。为了避免这一个冲突,就使用了“重载”功能</span>
<ul>
<li><span style="font-size: 16px"> 原型为:procedure RemoveDir(const ADirName:string);</span></li>
</ul>
</li>
<li><span style="font-size: 16px">Get: //从FTP服务器上下载文件.这个方法被重载了.</span></li>
<ul>
<li><span style="font-size: 16px"> 原型为:procedure Get(const ASourceFile:string;ADest:TStream);overload;</span></li>
<li><span style="font-size: 16px"> procedure Get(const ASourceFile:string;const ADest:string;ACanOverWrite:Boolean);overload;</span></li>
</ul>
<li><span style="font-size: 16px">Put: //用于向服务器上传文件.该方法被重载.KillDataChannal: //关闭FTP数据通道.</span>
<ul>
<li> 原型为:procedure Put(const ASourceFile:TStream;const ADest:string;const Appand:Boolean);overload;</li>
<li> procedure Put(const ASourceFile:string;const ADest:string;const AAppand:Boolean);overload;</li>
</ul>
</li>
<li><span style="font-size: 16px">Delete: //删除FTP服务器上一个文件.</span>
<ul>
<li> 原形为:procedure Delete(const AfileName:string);</li>
</ul>
</li>
<li><span style="font-size: 16px">Rename: //更改服务器上的文件名.</span>
<ul>
<li> 原型为:procedure Rename(const ASourceFile:string;const ADestFile:string);</li>
</ul>
</li>
<li><span style="font-size: 16px">Size: //获取文件大小信息.</span>
<ul>
<li> 原型为:function Size(const AFileName:string):Integer;</li>
</ul>
</li>
<li><span style="font-size: 16px">Delete: //删除FTP服务器上一个文件.</span>
<ul>
<li> 原形为:procedure Delete(const AfileName:string);</li>
</ul>
</li>
</ul>
<p> </p>
<p><strong><span style="font-size: 16px">3、代码示例:</span></strong><br><span style="font-size: 16px">//3.1连接</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">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;</span></pre>
</div>
<p> </p>
<p><span style="font-size: 16px">//3.2断开按钮的代码: </span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">try
if IdFTP.Connected then // 判断客户端是否连着服务器
IdFTP.Abort;
if TransferrignData then // 判断客户端与服务器之间是否有数据传输
IdFTP.Quit;
finally
IdFTP.Disconnect; // 断开连接
FTPCon.Enabled := true;
FTPDisCon.Enabled := false;
end;
</span></pre>
</div>
<p><span style="font-size: 16px"> </span></p>
<p><span style="font-size: 16px">//3.3TransferrignData 全局变量定义,连接按钮DisplayDir过程: </span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">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 then
DirectoryListBox.Items.Delete(0);
finally
LS.Free;
end;
end;
</span></pre>
</div>
<p><span style="font-size: 16px"> </span></p>
<p><span style="font-size: 16px">//3.4 显示目录(把LISTBOX的STYLE的属性改成lbOwnerDrawFixed,再在ONDRAWITEM方法里添 )</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">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 do
begin
//------------------------//
DirectoryListBox.Canvas.TextOut(Rect.Left, Rect.Top, FileName); // 文件名
R := Rect;
R.Left := Rect.Left + HeaderControl1.Sections.Items.Width;
//------------------------//
R.Right := R.Left + HeaderControl1.Sections.Items.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.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.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.Width;
DirectoryListBox.Canvas.FillRect(R);
DirectoryListBox.Canvas.TextOut(R.Left, Rect.Top, GroupName); // 组名
//------------------------//
R.Left := R.Right;
R.Right := R.Left + HeaderControl1.Sections.Items.Width;
DirectoryListBox.Canvas.FillRect(R);
DirectoryListBox.Canvas.TextOut(R.Left, Rect.Top, OwnerName); // 拥有者名字
//------------------------//
R.Left := R.Right;
R.Right := R.Left + HeaderControl1.Sections.Items.Width;
DirectoryListBox.Canvas.FillRect(R);
DirectoryListBox.Canvas.TextOut(R.Left, Rect.Top, OwnerPermissions + GroupPermissions + UserPermissions); // 权限
end;
end;
end;</span></pre>
</div>
<p><span style="font-size: 16px">提示:可以通过IDFTP里的work,status,workbegin,workend来实现进度条 </span><br><span style="font-size: 16px">//3.5 上传代码示例: </span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">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;
</span></pre>
</div>
<p><span style="font-size: 16px">//3.6 下载代码示例: </span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">procedure TFFTPClient.DirectoryListBoxDblClick(Sender: TObject);
var
FName: string;
begin
if not IdFTP.Connected then // 判断FTP是否还连着服务器
exit;
FName := IdFTP.DirectoryListing.Items.FileName;
if IdFTP.DirectoryListing.Items.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;
</span></pre>
</div>
<p><span style="font-size: 16px"> </span></p>
<p><span style="font-size: 16px">// 3.7 新建目录: </span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">var
DirName:string;
begin
DirName:=InputBox('输入新目录名称', '提示信息', '');
if DirName <> '' then
try
IdFTP.MakeDir(DirName);
DisplayDir(CURDIR.Text);
finally
end;
end;
</span></pre>
</div>
<p><span style="font-size: 16px"> </span></p>
<p> </p>
<p> </p>
<p><span style="font-size: 12px; color: rgba(136, 136, 136, 1)">创建时间:2020.05.08 更新时间:</span></p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!<br><br>
来源:https://www.cnblogs.com/guorongtao/p/12849327.html
頁:
[1]