在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Delphi IdFTP[1] 使用介绍
3、代码示例: 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断开按钮的代码: 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过程: 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方法里添 ) 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来实现进度条 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 下载代码示例: 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 新建目录: var
DirName:string;
begin
DirName:=InputBox('输入新目录名称', '提示信息', '');
if DirName <> '' then
try
IdFTP.MakeDir(DirName);
DisplayDir(CURDIR.Text);
finally
end;
end;
创建时间:2020.05.08 更新时间:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论