Title: FTP Up and Download with Indy
Question: Shows compile or script based file-transfer
Answer:
//**************************************************************************
program FastFTP_Up_Downloader_MAX;
// file read & write and FTP PUT / GET function , loc's = 93
// shows subroutines of assign file direct from filesystem to a FTP Upload!
// set the file direct with PATH AND HOST
const
E6= 1000000;
//FILE_PATH = 'E:\maxbox\maxbox3.zip';
//FILE_PATH = 'E:\maxbox\maxbox3\maxbox_functions.pdf';
//FILE_PATH = 'E:\maxbox\maxbox3\docs\maxblog.txt';
FILE_PATH = 'E:\maxbox\maxbox3\docs\maxboxblog.htm';
const
CHOSTNAME = 'www.softwareschule.ch';
CUSERNAME = '';
CPASSWORD = '';
procedure FTP_Upload(myFile: string);
var
ftpUpStream: TFileStream;
myftp: TIdFTP;
begin
//TFileStream.Create(afilename, fmOpenRead or fmShareCompat)
//ftpUpStream:= TFileStream.create(ExePath+'examples/'+myFile, fmOpenRead)
ftpUpStream:= TFileStream.create(FILE_PATH, fmopenread)
myftp:= TIdFTP.create(self);
try
with myftp do begin
Host:= CHOSTNAME;
Username:= CUSERNAME;
Password:= CPASSWORD;
end
//Connect FTP server and Use PASV mode
myftp.Connect(true, 1200)
myftp.Passive:= true;
//Change directory and Upload
//myftp.ChangeDir('httpdocs/download')
myftp.ChangeDir('httpdocs/images')
myftp.Put1(ftpUpStream, myFile, false);
writeln(inttoStr(myftp.size(myfile)))
finally
ftpUpStream.Free;
//Disconnect to Quit();
myftp.Quit;
myftp.Free;
//test finally
maxForm1.color:= clBlue;
end;
end;
procedure FTP_Download(myFile: string);
var
ftpDownStream: TFileStream;
myftp: TIdFTP;
begin
ftpDownStream:= TFileStream.create(myFile, fmCreate)
myftp:= TIdFTP.create(self);
try
with myftp do begin
Host:= CHOSTNAME;
Username:= CUSERNAME;
Password:= CPASSWORD;
end
myftp.Connect(true, 1200)
myftp.Passive:= true;
//myftp.filestructure
myftp.ChangeDir('httpdocs/images')
//download
myftp.Get1(myFile, ftpDownStream, false);
writeln(inttoStr(myftp.size(myfile)))
finally
ftpDownStream.Free;
myftp.Quit;
myftp.Free;
maxForm1.color:= clRed;
end;
end;
//main:
begin
Writeln(extractFilename(FILE_PATH))
//FTP_UPload(extractFilename(FILE_PATH));
FTP_DOWNload(extractfilename(FILE_PATH));
//myftp.Get2(MY_FTP_FILE, getFileStr, true, false);
end.
//Get and Put are overload methods, in maXbox you have to name it with Get1 and Get2 and so on.
--------------------------------------------------------------------------
{RegisterMethod('Procedure Put1( const ASource : TStream; const ADestFile:
string; const AAppend : boolean);');}
ftp.Host = "www.yourftpserveraddress.ch"
ftp.Username = "YourFtpUsername"
ftp.Password = "YourFtpPassword"