Search code examples
windowsftplazarus

Is there any simple way to use the FTP function in Lazarus code


I am a couple of months new to Lazarus. I have been trying to create a small FTP program that will send a small file after logging in. I have all the gooey stuff done and my only concern is the FTP part. I am getting a whole lot of errors and I have struggled to install the correct packages

My FTP code looks like this

function TModel.Send(LocalFile : string; remoteFile : string; RemoteDir : string) : boolean;
//===========================================================================
//     **********************************************************************
//     * Send a file to the FTP server                                      *
//     **********************************************************************
//---------------------------------------------------------------------------
var
   rc : boolean;
begin
   // Create the FTP Client object and set the FTP parameters
   FTPClient := TFTPSend.Create;
   with FTPClient do begin
      TargetPort  := cFtpProtocol;
      TargetHost := fHost;  // these were properties set somewhere else
      UserName := fUserID;
      Password := fPassword;
      //-----------------------------------------------------------------------
      // bail out if the FTP connect fails
      if not LogIn then exit;
      //------------------------------------------------------------------------

      // Set filename to FTP
      DirectFileName := LocalFile;
      DirectFile := True;
      //------------------------------------------------------------------------

      // change directory if requested
      if RemoteDir <> '' then ChangeWorkingDir(RemoteDir);
      //------------------------------------------------------------------------

      // STOR file to FTP server.  
      rc := StoreFile(RemoteFile,false); 
      //------------------------------------------------------------------------

      // close the connection
      LogOut;
      //------------------------------------------------------------------------
      // free the FTP client object
      free;
      //------------------------------------------------------------------------
   end;
   Result := rc;
//===========================================================================
end;

Thanks for your help.


Solution

  • Oh Lazarus XD. I'm not sure if there is any easy way though. I tried to do something similar a while back but i didn't get round to finishing it though.... But i did get the FTP to work take a look at my code below

     begin
      IdSMTP := TIdSMTP.Create(nil);
      try
        IdSMTP.Host := 'smtp.jonas.com';
        IdSMTP.Port := 587;
        IdSMTP.AuthType := satDefault;
        IdSMTP.Username := 'server@jonas.com';
        IdSMTP.Password := 'TeCat#!';
        IdSMTP.Connect;
        if IdSMTP.Authenticate then;
        begin
          IdMessage := TIdMessage.Create(nil);
          try
            IdMessage.From.Name := 'Jonas Server';
            IdMessage.From.Address := 'server@jonas.com';
            IdMessage.Subject := subject;
            IdMessage.Body.AddStrings(message);
            IdEmailAddressItem := IdMessage.Recipients.Add;
            IdEmailAddressItem.Address := 'server@jonas.com';
    
            IdSMTP.Send(IdMessage);
          finally
            IdMessage.Free;
          end;
        end;
        IdSMTP.Disconnect;
      finally
        IdSMTP.Free;
      end;
    end;
    

    I see you are using Synapse I can't remember what I used.... Its between indy, lnet or synapse. Just let me know if you need these packages I got them saved on my dropbox :) also check out THIS website it's a whole site dedicated to Laz.....GREAT ( ͡° ͜ʖ ͡°)