Hi Lars,

Here's some code that should be enough to get you started.  The code is
from a Gambas class which I called RequestManager.  I have cut out some
of the code out as it wasn't relevant to FTP (there was stuff for doing
an HTTP GET and some code for querying services on a particular port).

ISTR there was some flakiness with the FTP client where it bombs out if
you send a blank (empty string) password, so if your FTP server doesn't
require one, just set the FTP client's password to "dummy" or something
and your FTP server *should* ignore it... failing that, you may have to
set a dummy password.

Note the difference between the local file name and the name of the file
you want to get off the server.  The name of the file on the server is
normally included in the url set on FTPClient.URL (see the calling
instructions) and the name of the file on your local machine is passed
to the FTPClient.get method.  If you want to PUT files on the FTP server
instead of GETting them, I'm sure you can adapt the code.

Bear in mind that getting a huge binary file as a string is probably not
a great idea.  My getFileAsString method is really designed to work for
a small textual line-based file.  You'll just need to wrap getFileByFTP
in a different way if you want to deal with binary or larger files.
Give me a shout if you get stuck or something is not clear.

Enough rambling... to use the code, just instantiate a new
RequestManager (or whatever you call the class) and use it as follows:

PRIVATE myRequestManager AS NEW RequestManager

...
DIM allData AS String
allData =
myRequestManager.getFileAsString("192.168.1.2//var/share/dvb2001/services.dat", 
"root", "password")


Now here follows (some of) the code for the RequestManager class itself:

' Gambas class file
PRIVATE theFTPClient AS NEW FtpClient
PRIVATE eggTimer AS NEW Timer
PRIVATE timedOut AS Boolean
PRIVATE busy AS Boolean
PRIVATE allStates AS Collection

PRIVATE SUB getFileByFTP(url AS String, userName AS String, password AS
String, localFile AS String)
  
  DIM fileName AS String
  
  theFTPClient.URL = url
  theFTPClient.User = userName
  theFTPClient.Password = password
  secondsBeforeTimeout(10)
  theFTPClient.get(localFile)
  DO UNTIL theFTPClient.Status <= 0 OR timedOut = TRUE
    Logger.logMessage("RequestManager.getFileByFTP() status: " &
theFTPClient.Status, FALSE)
    WAIT 0.01
  LOOP
  IF theFTPClient.Status <> 0 THEN
    message("Error in FTP call!  Check password?")
  END IF
  busy = FALSE
CATCH
  Logger.logMessage("getFileByFTP: " & Error.Text & " @ " & Error.Where,
TRUE)
END

PUBLIC FUNCTION getFileAsString(url AS String, userName AS String,
password AS String, tmpDir AS String) AS String

  DIM result AS String  
  DIM fileName AS String
  DIM hFile AS File
  DIM aLine AS String
  fileName = tmpDir &/ "drag_cont_tmp_" & userName & ".tmp"
  Logger.logMessage("RequestManager.getFileAsString() fileName: " &
fileName, FALSE)
  getFileByFTP(url, userName, password, fileName)
  result = ""
  OPEN fileName FOR READ AS #hFile
  WHILE NOT Eof(hFile)
    LINE INPUT #hFile, aLine
    IF aLine <> NULL THEN
      result = result & aLine & gb.NewLine
    END IF
  WEND
  CLOSE #hFile
  RETURN result
CATCH
  Logger.logMessage("getFileByFTPAsString: " & Error.Text & " @ " &
Error.Where, TRUE)
END


PUBLIC SUB eggTimer_Timer()
  
  eggTimer.Enabled = FALSE
  timedOut = TRUE
  Object.Detach(eggTimer)
  
END

PUBLIC FUNCTION isBusy() AS Boolean
  
  Logger.logMessage("RequestManager checked for busy state and was: " &
booleanToString(busy), FALSE)
  RETURN busy
  
END

PRIVATE SUB secondsBeforeTimeout(seconds AS Integer)
  
  Object.Attach(eggTimer, ME, "eggTimer")
  timedOut = FALSE
  eggTimer.Delay = seconds * 1000
  busy = TRUE
  eggTimer.Enabled = TRUE
  
END

PRIVATE FUNCTION booleanToString(value AS Boolean) AS String
  
  IF value THEN
    RETURN "TRUE"
  ELSE
    RETURN "FALSE"
  END IF
  
END

PRIVATE FUNCTION getState(aState AS Integer) AS String
  
  RETURN getAllStates()[aState]
  
END

PRIVATE FUNCTION getAllStates() AS Collection
  
  IF allStates = NULL THEN
    allStates = NEW Collection
    allStates.Add("Accepting", Net.Accepting)
    allStates.Add("Active", Net.Active)
    allStates.Add("CannotBindSocket", Net.CannotBindSocket)
    allStates.Add("CannotCreateSocket", Net.CannotCreateSocket)
    allStates.Add("CannotListen", Net.CannotListen)
    allStates.Add("CannotRead", Net.CannotRead)
    allStates.Add("CannotWrite", Net.CannotWrite)
    allStates.Add("Connected", Net.Connected)
    allStates.Add("Connecting", Net.Connecting)
    allStates.Add("ConnectionRefused", Net.ConnectionRefused)
    allStates.Add("HostNotFound", Net.HostNotFound)
    allStates.Add("Inactive", Net.Inactive)
    allStates.Add("Internet", Net.Internet)
    allStates.Add("IPv4", Net.IPv4)
    allStates.Add("Local", Net.Local)
    allStates.Add("Pending", Net.Pending)
    allStates.Add("ReceivingData", Net.ReceivingData)
    allStates.Add("Searching", Net.Searching)
    allStates.Add("Unix", Net.Unix)
  END IF
  RETURN allStates
  
END

On Thu, 2009-09-03 at 14:24 +0200, Lars Hoeger wrote:
> Hello,
> 
> I would like to use the ftpclient component, but there is no documentation. 
> Has anyone some code-examples?
> 
> Thanks,
> Lars
> gambas 2.15
> 
> 
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
> trial. Simplify your report design, integration and deployment - and focus on 
> what you do best, core application coding. Discover what's new with 
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> Gambas-user mailing list
> Gambas-user@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Gambas-user mailing list
Gambas-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user

Reply via email to