Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Indy Trivial FTP Client/Server Example

Posted on 2004-09-13
5
Medium Priority
?
4,897 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

610 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