Skip to content

Instantly share code, notes, and snippets.

@NoNameDev-Git
Last active June 28, 2023 15:50
Show Gist options
  • Save NoNameDev-Git/219ce94a3f86b18fa4f2f8441b45b502 to your computer and use it in GitHub Desktop.
Save NoNameDev-Git/219ce94a3f86b18fa4f2f8441b45b502 to your computer and use it in GitHub Desktop.
Delphi XE7 FTP Upload and Download File
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons,
IdFTP, IdFTPCommon, IdFTPList;
type
TForm1 = class(TForm)
SpeedButton1: TSpeedButton;
Memo1: TMemo;
procedure SpeedButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const
user32 = 'user32.dll';
shell32 = 'shell32.dll';
implementation
{$R *.dfm}
function Wow64DisableWow64FsRedirection(Var Wow64FsEnableRedirection: LongBool): LongBool; stdcall;
External kernel32 name 'Wow64DisableWow64FsRedirection';
function ShellExecuteW(hWnd: THandle; Operation, FileName, Parameters, Directory: WideString; ShowCmd: Integer): HINST; stdcall;
external shell32 name 'ShellExecuteW';
//Method for launching files
procedure ShellExecute(hWnd: THandle; Operation, FileName, Parameters, Directory: WideString; ShowCmd: Integer);
var
WFER: LongBool;
begin
if Wow64DisableWow64FsRedirection(WFER) then
ShellExecuteW(hWnd, Operation, FileName, Parameters, Directory, ShowCmd)
else ShellExecuteW(hWnd, Operation, FileName, Parameters, Directory, ShowCmd);
end;
//Add a program to a firewall exception
procedure AddToFirewall(FileName:string);
var
SP1:string;
begin
SP1 := '/c netsh advfirewall firewall add rule name="'+ExtractFileName(FileName)+'" dir=in action=allow program="'+FileName+'" enable=yes';
ShellExecute(0, 'open', PChar('cmd.exe'), PChar(SP1), '', 0);
end;
//Get a list of files and directories
function FTPGetListDir(host:string; port:integer; user,pass,dir:string):string;
var
ftp:TIdFTP;
list:TStringList;
i:Integer;
begin
ftp := TIdFTP.Create;
list := TStringList.Create;
try
ftp.host := host;
ftp.username := user;
ftp.password := pass;
ftp.Port := port;
ftp.Connect;
if ftp.Connected then
begin
ftp.ChangeDir(dir);
ftp.List('*',true);
for i:=0 to ftp.directorylisting.Count-1 do
begin
if((ftp.DirectoryListing[I].ItemType = ditDirectory) and
(ftp.DirectoryListing[I].FileName <> '.') and
(ftp.DirectoryListing[I].FileName <> '..')) then
begin
list.Add(ftp.DirectoryListing[I].FileName);
end else if( ftp.DirectoryListing[I].ItemType = ditFile) then
begin
list.Add(ftp.DirectoryListing[I].FileName);
end;
end;
Result := list.Text;
end;
finally
ftp.Disconnect;
ftp.Free;
list.Free;
end;
end;
//Upload Binary file to FTP server
function FTPPutFile(host:string; port:integer; user, pass, dir, pathfile:string):boolean;
var
ftp:TIdFTP;
begin
Result := False;
ftp := TIdFTP.Create;
try
ftp.host := host;
ftp.username := user;
ftp.password := pass;
ftp.Port := port;
ftp.TransferType:=ftBinary; //or ftASCII on text files
ftp.Connect;
if ftp.Connected then
begin
ftp.ChangeDir(dir);
ftp.List('*',true);
ftp.Put(pathfile,ExtractFileName(pathfile),false);
Result := True;
end;
finally
ftp.Disconnect;
ftp.Free;
end;
end;
//Download file from FTP server
function FTPGetFile(host:string; port:integer; user, pass, dir, ftpfile, pathfile:string):boolean;
var
ftp:TIdFTP;
begin
Result := False;
ftp := TIdFTP.Create;
try
ftp.host := host;
ftp.username := user;
ftp.password := pass;
ftp.Port := port;
ftp.TransferType:=ftBinary; //or ftASCII on text files
ftp.Connect;
if ftp.Connected then
begin
ftp.ChangeDir(dir);
ftp.Get(ftpfile,pathfile,true);
Result := True;
end;
finally
ftp.Disconnect;
ftp.Free;
end;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
//Adding our program to the firewall
AddToFirewall(ParamStr(0));
//Upload file to FTP server
if FTPPutFile('192.168.100.5', 21, 'ftp','ftp','/www/','C:\ZEOSDBO.zip') = True then
ShowMessage('The file has been delivered to the FTP server.');
//Get a list of files and directories
Memo1.Text := FTPGetListDir('192.168.100.5', 21, 'ftp','ftp','/www/');
//Download file from FTP server
if FTPGetFile('192.168.100.5', 21, 'ftp','ftp','/www/','ZEOSDBO.zip','C:\ZEOSDBO111.zip') = True then
ShowMessage('The file was downloaded from the FTP server.');
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment