Recent

Author Topic: FTP  (Read 9846 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2156
    • Lazarus and Free Pascal italian community
FTP
« on: February 17, 2016, 10:20:18 pm »
There is a working example of an ftp file transfer?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

JanRoza

  • Hero Member
  • *****
  • Posts: 617
    • http://www.silentwings.nl
Re: FTP
« Reply #1 on: February 18, 2016, 12:14:36 am »
Something like this?

Code: Pascal  [Select][+][-]
  1. uses FTPSend;
  2.  
  3. var
  4.   strFtpIni: String = '/httpdocs/<Folder name>/<File name>';
  5.   strFTPHost: String = 'server.domain.com';
  6.   strFTPuser: String = '<User name>';
  7.   strFTPpw: String = '<Password>';
  8.  
  9.   with TFTPSend.Create do
  10.   try
  11.     Username := strFTPuser;
  12.     Password := strFTPpw;
  13.     TargetHost := strFTPHost;
  14.     TargetPort := '21';
  15.  
  16.     DirectFileName := <Target filename>;
  17.     DirectFile := True;
  18.     RetrieveFile(strFtpIni, False);
  19.     end;
  20.   finally
  21.     Free;
  22.   end;
  23.  
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
Laz: Lazarus 2.0.12 FPC 3.2.0
       CodeTyphon 7.5 FPC 3.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 10795
Re: FTP
« Reply #2 on: February 18, 2016, 09:42:48 am »
Or even this (also with synapse)?
Code: Pascal  [Select][+][-]
  1. program simpleftp;
  2. {$apptype console}{$ifdef fpc}{$mode delphi}{$endif}
  3. uses ftpsend;
  4. begin
  5.   if ftpGetFile('<domain_or_ip>','21','<remote_file_name_plus_path>',
  6.                      '<target_filename_plus_path>',
  7.                      '<username'>,'<password>') then
  8.     writeln('succes') else writeln('fail');
  9.   readln;
  10. end.
  11.  

If you want to send a file, just change ftpGetFile into ftpPutFile. The syntax is the same.
« Last Edit: February 18, 2016, 12:13:22 pm by Thaddy »

xinyiman

  • Hero Member
  • *****
  • Posts: 2156
    • Lazarus and Free Pascal italian community
Re: FTP
« Reply #3 on: February 18, 2016, 10:48:55 pm »
I tried with this function and goes well with individual files. But if I want to send an entire directory with subdirectories?

Code: Pascal  [Select][+][-]
  1. function TForm1.Send(fHost: string; fUserID: string; fPassword: string; LocalFile : string; remoteFile : string; RemoteDir : string) : boolean;
  2. //===========================================================================
  3. //     **********************************************************************
  4. //     * Send a file to the FTP server                                      *
  5. //     **********************************************************************
  6. //---------------------------------------------------------------------------
  7. var
  8.    rc : boolean;
  9.    FTPClient : TFTPSend;
  10. begin
  11.    // Create the FTP Client object and set the FTP parameters
  12.    FTPClient := TFTPSend.Create;
  13.    with FTPClient do begin
  14.       TargetPort  := '21';
  15.       TargetHost := fHost;  // these were properties set somewhere else
  16.       UserName := fUserID;
  17.       Password := fPassword;
  18.       //-----------------------------------------------------------------------
  19.       // bail out if the FTP connect fails
  20.       if not LogIn then exit;
  21.       //------------------------------------------------------------------------
  22.  
  23.       // Set filename to FTP
  24.       DirectFileName := LocalFile;
  25.       DirectFile := True;
  26.       //------------------------------------------------------------------------
  27.  
  28.       // change directory if requested
  29.       if RemoteDir <> '' then ChangeWorkingDir(RemoteDir);
  30.       //------------------------------------------------------------------------
  31.  
  32.       // STOR file to FTP server.
  33.       rc := StoreFile(RemoteFile,false);
  34.       //------------------------------------------------------------------------
  35.  
  36.       // close the connection
  37.       LogOut;
  38.       //------------------------------------------------------------------------
  39.       // free the FTP client object
  40.       free;
  41.       //------------------------------------------------------------------------
  42.    end;
  43.    Result := rc;
  44. //===========================================================================
  45. end;
  46.  
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 639
Re: FTP
« Reply #4 on: February 22, 2016, 05:24:24 pm »
Use FindAllFiles to make a list.

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: FTP
« Reply #5 on: February 24, 2016, 01:37:14 pm »
There are many examples of FTP usage in these forums. Please use the Search  functionality to find more examples using FPC only, Synapse, Indy 10 etc.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

evresis

  • Newbie
  • Posts: 1
Re: FTP
« Reply #6 on: June 09, 2021, 10:20:17 pm »
What happens if the command PASV (when FTP.Passive:=true) is returned to a private address by mistake on the remote server firewall config? eg'227 Entering Passive Mode (192,168,148,37,129,44)'. We receiving error 425 can't open data connection! I am using synapse 4.0 and Lazarus 2.0.10. In most famous ftp clients (filezilla,winscp etc) user can replace or better force the public ip of remote server before send a data command to ftp server. With few code line addition to Class TFTPSend in unit ftpsend we can reestablish lost data connection.
  • a) Add a boolean variable to protected part of class eg
Code: Pascal  [Select][+][-]
  1. FForceIP: Boolean;
  • b) Add a boolean property to published part of class eg
Code: Pascal  [Select][+][-]
  1. property ForceIP: Boolean read FForceIP write FForceIP;
permits to change the above variable
  • c) At
Code: Pascal  [Select][+][-]
  1. function TFTPSend.DataSocket: boolean;
after the command
Code: Pascal  [Select][+][-]
  1. FDSock.CloseSocket;
in passive mode first part of function adding the following command
Code: Pascal  [Select][+][-]
  1. if FForceIP then FDataIP:=FTargetHost;
that replaces private IP given by PASV command with the IP of remote ftp server.
  • d) To enable force remote host IP address just set
Code: Pascal  [Select][+][-]
  1. FTP.Passive:= True;
and
Code: Pascal  [Select][+][-]
  1. FTP.ForceIP:= True;
    . Τhe data channel is now returning and operating normally!

 

TinyPortal © 2005-2018