|
|
|
|
Apro 3 – FTP: Remote Directory Picker
The TApdFtpClient component in Apro version 3.0 makes it easy to transfer FTP files from within a Delphi or C++Builder application. That is, assuming you know the name of the file and directory on the remote FTP server. However, this too is handled easily with the TApdFtpClient. To illustrate, this article presents an example project that shows how to automatically display the contents of the current working directory in a TMemo, and navigate through the directory tree by double-clicking on entries in the memo. The following figure shows the form of the example at run-time. Start off by dropping a TApdFtpClient, TMemo, and three TButton’s onto the main form of a new application. You can set the ServerAddress, UserName, and Password properties of the TApdFtpClient component at design time via the object inspector, or set them at run time from TEdit controls as I’ve done with this example. At any rate, these need to be set prior to attempting to login to an FTP server. The following are event handlers I’ve defined for the OnClick event for the Login, Logout, and Close buttons: Delphi:procedure TForm1.btnLoginClick(Sender: TObject); begin with ApdFtpClient1 do begin ServerAddress := edtServer.Text; UserName := edtUser.Text; Password := edtPassword.Text; Login; end; end;
procedure TForm1.btnLogoutClick(Sender: TObject); begin ApdFtpClient1.Logout; end;
procedure TForm1.btnCloseClick(Sender: TObject); begin ApdFtpClient1.Logout; Close; end;
C++Builder:void __fastcall TForm1::btnLoginClick(TObject *Sender) { ApdFtpClient1->ServerAddress = edtServer->Text; ApdFtpClient1->UserName = edtUser->Text; ApdFtpClient1->Password = edtPassword->Text; ApdFtpClient1->Login(); }
void __fastcall TForm1::btnLogoutClick(TObject *Sender) { ApdFtpClient1->Logout(); }
void __fastcall TForm1::btnCloseClick(TObject *Sender) { ApdFtpClient1->Logout(); Close(); }
Once successfully logged in, we’ll display the path of server’s current working directory in the form’s caption and its contents in the memo. Also, we want to refresh these when moving from one directory to another. For this I use the OnFtpStatus event of the TApdFtpClient since we need to allow one FTP operation to complete before initiating a new one:
Delphi:
procedure TForm1.ApdFtpClient1FtpStatus(Sender: TObject; StatusCode: TFtpStatusCode; InfoText: PChar); begin case StatusCode of scLogin : ApdFtpClient1.CurrentDir; scLogout : begin lbxCurrentDir.Clear; Caption := 'ExFtpDir'; end; scComplete : ApdFtpClient1.CurrentDir; scCurrentDir : begin Caption := StrPas(InfoText); ApdFtpClient1.ListDir('', False); end; scDataAvail : with LbxCurrentDir.Items do begin Text := StrPas(InfoText); Insert(0, '..'); Insert(0, '.'); end; end; end;
C++Builder:
void __fastcall TForm1::ApdFtpClient1FtpStatus(TObject *Sender, TFtpStatusCode StatusCode, PChar InfoText) { switch (StatusCode) { case scLogin : ApdFtpClient1->CurrentDir(); break; case scLogout : lbxCurrentDir->Clear(); Caption = "ExFtpDir"; break; case scComplete : ApdFtpClient1->CurrentDir(); break; case scCurrentDir : Caption = InfoText; ApdFtpClient1->ListDir("", false); break; case scDataAvail : lbxCurrentDir->Items->Text = InfoText; lbxCurrentDir->Items->Insert(0, ".."); lbxCurrentDir->Items->Insert(0, "."); break; } }
Now, to change directories by double clicking I’ve defined the following event handlers for the TMemo:
Delphi:
var SelectedDir : string; {- global variable }
procedure TForm1.lbxCurrentDirMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var i : Integer; begin i := lbxCurrentDir.ItemAtPos(Point(X, Y), True); if (i > -1) then SelectedDir := lbxCurrentDir.Items[i]; end;
procedure TForm1.lbxCurrentDirDblClick(Sender: TObject); begin ApdFtpClient1.ChangeDir(SelectedDir); end;
C++Builder:
AnsiString SelectedDir; // global variable
void __fastcall TForm1::lbxCurrentDirMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { int i = lbxCurrentDir->ItemAtPos(Point(X, Y), true); if (i > -1) SelectedDir = lbxCurrentDir->Items->Strings[i]; }
void __fastcall TForm1::lbxCurrentDirDblClick(TObject *Sender) { ApdFtpClient1->ChangeDir(SelectedDir); }
Lastly, we’ll want some indication if a we cannot change to the specified directory. I do this with the following event handler for the OnFtpError event of the TApdFtpClient:
Delphi:
procedure TForm1.ApdFtpClient1FtpError(Sender: TObject; ErrorCode: Integer; ErrorText: PChar); begin Caption := StrPas(ErrorText); end;
C++Builder: void __fastcall TForm1::ApdFtpClient1FtpError(TObject *Sender, int ErrorCode, PChar ErrorText) { Caption = ErrorText; }
That’s all there is to it! A quick and easy directory picker that can be used as a starting point for any FTP file transfer application or as a basis for you’re own directory picker component.
|
This site is not affiliated, endorsed, or otherwise associated with the
entity formerly known as
TurboPower Software. The owners and maintainers of Aprozilla.com were merely
meager employees of the aforementioned organization, providing this site out of
the pure goodness of their collective hearts.
|