Solved

Indy Trivial FTP Client/Server Example

Posted on 2004-09-13
5
4,668 Views
Last Modified: 2012-08-14
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,

D2.
0
Comment
Question by:wavget
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 12046177
did you check the samples from the indy homepage ?
0
 
LVL 8

Expert Comment

by:BdLm
ID: 12046247
0
 
LVL 1

Author Comment

by:wavget
ID: 12053740
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 indyproject.org 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.
0
 
LVL 1

Accepted Solution

by:
NickRyan earned 100 total points
ID: 12063094
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 );
var
  EffectiveFileName:string;
const
  SERVER_ROOT = 'C:\TFTP\';
begin

  {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}
    False;
    Exit;
  end; {if}

  {Setup file access}
  try
    {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;
  except
    {Handle failure to open file stream here - ie, file locked}
    GrantAccess := False;
  end; {try}

end;
0
 
LVL 1

Expert Comment

by:NickRyan
ID: 12063281
Oops - please excuse the extra "False;" line in just after the FileExists check... it's just a typo.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 317
Convert MS Word document to a PDF file 9 91
RESTRequest Parameter 4 43
Twebbrowser in Fire monkey android scrolling to bottom 1 45
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question