Use the idFtp event in runtime

Hi Guys,

The idFtp has an important event that gets the results, I don't know how to set / use the event when I create the idFtp in runtime. I need to
an example for it.  Below you can see my code and use it to give me an example.

Thanks a lot

  Function RunFTP( FtpServidor ,  FtpUsuario, FtpSenha, FtpPorta,  FtpAutentica, FtpCaminhoUpload, FtpCaminhoResposta: String): Boolean;

  Result := True

  if EstaVazio(FtpServidor) or EstaVazio( FtpUsuario ) or EstaVazio( FtpSenha ) or  EstaVazio( FtpAutentica ) or
     EstaVazio( FtpPorta )  {or EstaVazio( SmtpCaminhoUpload ) or EstaVazio( SmpCaminhoResposta )} then Begin
     ShowMessage('As configurações de FTP não estão completas no parceiro: ' + CodCad);
     Result := False;
     Exit;
  End;


  if FtpAutentica = '1' then
     FtpAut  := true
  else
     FtpAut  := false;


  IdFTP1 := TIdFTP.Create();
  IdFTP1.Disconnect;

  IdFTP1.Host     := FtpServidor;
  idFTP1.DirectoryListing.DirectoryName:= FtpCaminhoUpload;
  IdFTP1.Port     := Strtoint(FtpPorta);
  IdFTP1.Username := FtpUsuario;
  IdFTP1.Password := FtpSenha;
  IdFTP1.Passive  := FtpAut;

  try
     IdFTP1.Connect;
  except
     IdFTP1.Disconnect;
     IdFTP1.Connect;
     Result := False;
     Exit;
  end;

  BarraRotina('UP',Carquivo+iif(Tipo = 'Upload',' Envio ',' Recebimento ')+' de Arquivo '+Carquivo,0,0);

  Try
    Try
      if Tipo = 'Upload' then begin
         IdFTP1.Put (cArquivo, FtpCaminhoUpload+Carquivo, true);
      end else if Tipo = 'Download' then begin
                  IdFTP1.Get ( FtpCaminhoResposta+Carquivo, Carquivo, true,false);
      end else if Tipo = 'Delete' then begin
                  IdFTP1.delete ( FtpCaminhoUpload+Carquivo);
      end;
    Except
      Result := false
    End;
  Finally
    BarraRotina('UPC','Arquivo '+Carquivo+iif(Tipo = 'Upload',' Enviado ',' Recebido ')+' com sucesso',0,0);
  End;

Open in new window

LVL 1
hidrauAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert GOracle dbaCommented:
the event is defined as a method "of object"
this means it has to be a method of an object

that means you need to create a wrapper object

removing the begin and end from a sample ???
that makes it harder to read

you'r trying to use oop language as procedural language
if you want to use objects, you need to follow oop rules

your code contains exits ... fine, but hard to follow
type
  TWrapper = class(TObject)
  private
    procedure IdFTP1Connected(Sender: TObject);
  public
    function RunFtp (FtpServidor ,  FtpUsuario, FtpSenha, FtpPorta,  FtpAutentica, FtpCaminhoUpload, FtpCaminhoResposta: String): Boolean;
    function EstaVazio(item: string): boolean;
  end;


Function TWrapper.RunFTP( FtpServidor ,  FtpUsuario, FtpSenha, FtpPorta,  FtpAutentica, FtpCaminhoUpload, FtpCaminhoResposta: String): Boolean;
begin

  Result := False;

  if EstaVazio(FtpServidor) or EstaVazio( FtpUsuario ) or EstaVazio( FtpSenha ) or  EstaVazio( FtpAutentica ) or
     EstaVazio( FtpPorta )  {or EstaVazio( SmtpCaminhoUpload ) or EstaVazio( SmpCaminhoResposta )} then
  Begin
     ShowMessage('As configurações de FTP não estão completas no parceiro: ' + CodCad);
  End;


  if FtpAutentica = '1' then
     FtpAut  := true
  else
     FtpAut  := false;


  IdFTP1 := TIdFTP.Create();
  // IdFTP1.Disconnect; not necessary

  IdFTP1.Host     := FtpServidor;
  idFTP1.DirectoryListing.DirectoryName:= FtpCaminhoUpload;
  IdFTP1.Port     := Strtoint(FtpPorta);
  IdFTP1.Username := FtpUsuario;
  IdFTP1.Password := FtpSenha;
  IdFTP1.Passive  := FtpAut;
  idFtp1.OnConnected := IdFTP1Connected;

  try
     IdFTP1.Connect;
  except
     // IdFTP1.Disconnect; not necessary ... you're not connected
     // IdFTP1.Connect; ??? you caugt the connection error and try again anyway ??? why ?
     // log a message :
     on e: exception do
      LogMessage(E.Exception);
  end;

  // ???
  BarraRotina('UP',Carquivo+iif(Tipo = 'Upload',' Envio ',' Recebimento ')+' de Arquivo '+Carquivo,0,0);

  Try
    Try
      if Tipo = 'Upload' then begin
         IdFTP1.Put (cArquivo, FtpCaminhoUpload+Carquivo, true);
      end else if Tipo = 'Download' then begin
                  IdFTP1.Get ( FtpCaminhoResposta+Carquivo, Carquivo, true,false);
      end else if Tipo = 'Delete' then begin
                  IdFTP1.delete ( FtpCaminhoUpload+Carquivo);
      end;
    Except
      Result := false
    End;
  Finally
    BarraRotina('UPC','Arquivo '+Carquivo+iif(Tipo = 'Upload',' Enviado ',' Recebido ')+' com sucesso',0,0);
  End;

  // free idftp1 ???

end;

procedure TWrapper.IdFTP1Connected(Sender: TObject);
begin
  // connected !

end;

Open in new window


still needs code, but it's to explain the idea

you can use it in your code like this
procedure TForm1.Button1Click(Sender: TObject);
var w: TWrapper;
begin
  w := TWrapper.Create;
  try
    w.RunFtp(... );
  finally
    w.Free;
  end;
end;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hidrauAuthor Commented:
THANKS A LOT FOR YOUR HELP
hidrauAuthor Commented:
THANKS
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.