使用控件:IdFTP1
1.连接
procedure TmainForm.Button1Click(Sender: TObject);
begin
idftp1.Host:=edit1.Text;
idftp1.Username:=edit2.Text;
idftp1.Password:=edit3.Text;
idftp1.Connect(true,-1);
if idftp1.Connected then
begin
showmessage('connected!');
button6.Click;
end
else
begin
showmessage('not connected!');
end;
end;
2.断开
procedure TmainForm.Button3Click(Sender: TObject);
begin
idftp1.Disconnect;
end;
3.文件列表
procedure TmainForm.Button6Click(Sender: TObject);
var
tr:tstrings;
begin
tr := TStringlist.Create;
Edit4.Text := IdFTP1.RetrieveCurrentDir;//得到初始目录
IdFTP1.List(tr); //得到client目录下所有文件列表
Memo1.Lines.Assign(tr);
tr.Free;
end;
4.进入子目录
procedure TmainForm.Button4Click(Sender: TObject);
begin
IdFTP1.ChangeDir(edit5.Text); //进入到client子目录
button6.Click;
end;
5.返回上级目录
procedure TmainForm.Button5Click(Sender: TObject);
begin
IdFTP1.ChangeDir('..'); //回到上一级目录
button6.Click;
end;
6.下载
procedure TmainForm.Button8Click(Sender: TObject);
var
bytestotransfer:integer;
name:string;
begin
name:=edit6.Text;
savedialog1.FileName:=name;
if SaveDialog1.Execute then
begin
// IdFTP1.TransferType := ftBinary;
BytesToTransfer := IdFTP1.Size(Name);
if FileExists(Name) then
begin
case MessageDlg('File aready exists. Do you want to resume the download operation?',mtConfirmation, mbYesNoCancel, 0) of
mrYes:
begin
// BytesToTransfer := BytesToTransfer - FileSizeByName(Name);
IdFTP1.Get(Name, SaveDialog1.FileName, false, true);
showmessage('文件下载完毕!');
end;
mrNo:
begin
IdFTP1.Get(Name, SaveDialog1.FileName, true);
showmessage('文件下载完毕!');
end;
mrCancel:
begin
exit;
end;
end;
end
else
begin
IdFTP1.Get(Name, SaveDialog1.FileName, false);
showmessage('文件下载完毕!');
end;
end;
end;
7.上传、下载简单示例
上传:
IdFTP1.Host := '主机地址';
IdFTP1.Username := '用户名';
IdFTP1.Password := '密码';
if IdFTP1.Connected then
IdFTP1.Disconnect;
IdFTP1.Connect();
try
IdFTP1.Put('data/本地文件名','远程文件名');
except
...
下载:
IdFTP1.Host := '主机地址';
IdFTP1.Username := '用户名';
IdFTP1.Password := '密码';
if IdFTP1.Connected then
IdFTP1.Disconnect;
IdFTP1.Connect();
try
IdFTP1.Get('\data\远程文件名','本地文件名',True);
except
...
阅读(1001) | 评论(0) | 转发(0) |