Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5094
  • Last Modified:

Indy Trivial FTP Client/Server Example

I'm looking for an example on how to use the Indy Trivial FTP client and server components to upload and download a file.

Get and Put with the client seems clear enough, but I'm unsure how to handle the requests in te server.

Thanks in advance,

  • 2
  • 2
1 Solution
did you check the samples from the indy homepage ?
wavgetAuthor Commented:
First of all, Delphi comes with an example of both a TrivialFtp Server and Client, I just didn't think to look there at first :)

I downloaded the demo from but I am unable to find a TrivialFtp example in it.

My biggest question is how do I handle OnRead and OnWrite in the server.
Here's a short sample of how you could handle the file read process. The file write process is pretty similar except that you may want to put in more checks in the case of overwriting files.

This short example assumes that you're only accepting filenames, not filepaths in the TFTP server - if you want to accept file paths as well then you need to process the directory separators and ensure that it's not possible for a user to reference files outside of where you want them to. ie, strip out any "..\" items in the path.

You can use the IP address of the client as a form of access control if you need or want to restrict access to certain IP addresses or address ranges.

procedure TForm1.IdTrivialFTPServer1ReadFile( Sender:TObject;
  var FileName:String; const PeerInfo:TPeerInfo;
  var GrantAccess:Boolean; var AStream:TStream;
  var FreeStreamOnComplete:Boolean );

  {First, check IP address of client, if this is what you need to do}
  // PeerInfo.PeerIP is the IP address of the client
  // if not PeerInfo.PeerIP = AllowedIPAddress then begin
  //   GrantAccess := False;
  //   Exit;
  // end; {if}

  {Process filename - in this case we assume that we are dealing with just a filename with no dir part}
  EffectiveFileName := SERVER_ROOT + ExtractFileName(FileName);
  GrantAccess := FileExists(EffectiveFileName);
  if not GrantAccess then begin
    {File doesn't exist, so obviously can't allow access}
  end; {if}

  {Setup file access}
    {Open a file stream referencing the file}
    AStream := TFileStream.Create( EffectiveFileName, fmOpenRead or fmShareDenyWrite );
    {Allow the server FTP component to destroy the stream when the transfer process finishes}
    FreeStreamOnComplete := True;
    {Handle failure to open file stream here - ie, file locked}
    GrantAccess := False;
  end; {try}

Oops - please excuse the extra "False;" line in just after the FileExists check... it's just a typo.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now