Upload/Download Files

Post Reply
User avatar
Jeff Barnes
Posts: 933
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada
Contact:

Upload/Download Files

Post by Jeff Barnes »

Hi Everybody,

I am trying (with no luck) to send a file from my desktop to my webserver via sockets.

If I try the sockcli.prg and sockserv.prg locally (127.0.0.1) it works.
When I try my webserver IP nothing gets sent.

Is there any other way to upload a file to a website and download a file from a website?
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Post by Antonio Linares »

Jeff,

> When I try my webserver IP nothing gets sent.

Your (pc or office) firewall may be blocking it

> Is there any other way to upload a file to a website and download a file from a website?

Please review source\classes\TFtpCli.prg to use a FTP client conection to upload files (Method Stor()) or to download files (Method RetR())

also please review FWH\samples\gallery\icopyfil\icopyfil.prg
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Jeff Barnes
Posts: 933
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada
Contact:

Post by Jeff Barnes »

Antonio,

This FtpSendFiles() and FtpGetFiles() is exactly what I need.
However, if I try the code below it will not connect. I even tried without the user:pass (thought the ftp site would ask for it) but still will not connect.

I have tried with and without the "ftp://" added to the string.
I also tried replacing the myweeb.net with the actual IP address.


If I enter the ftp:// info in my browser it does connect so I know it's not an issue with the ftp site.


FTPSendFiles( "user:pass@myweb.net",{ "c:\FTPSENDTEST.TXT"}, { "\FTPSENDTEST1.TXT"} )

Any ideas?


Also, Is there a ftp version of FILE() to check and see if a file in available on the ftp site?
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Jeff Barnes
Posts: 933
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada
Contact:

Post by Jeff Barnes »

I also tried adding the user/pass as below:

oFTP := TFTP():New( cFTPSite, oInternet, "user", "pass" )
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Jeff Barnes
Posts: 933
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada
Contact:

Post by Jeff Barnes »

I've got the upload / download working now ... it was a router issue.


All I need to know now is .... is there an FTP function to check and see if a file is available for download :D
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Otto
Posts: 6403
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 22 times
Been thanked: 2 times
Contact:

Post by Otto »

Hello Jeff,
this is what I use.
Regards,
Otto


Code: Select all | Expand

#include "FiveWin.ch"

//----------------------------------------------------------------------------//

function Main()
   local oWnd, oTmr

   DEFINE WINDOW oWnd TITLE "Timers"

   DEFINE TIMER oTmr  INTERVAL 10000 ACTION  (oTmr:DeActivate(),oWnd:Say( 10, 17, Time() ), download(),oTmr:Activate() ) of oWnd
   ACTIVATE TIMER oTmr

   ACTIVATE WINDOW oWnd ;
      ON CLICK MsgInfo( "Click!" )

   oTmr:End()

return nil

//----------------------------------------------------------------------------//

FUNC download()
   local oInternet := TInternet():New()
   local oFTP      := TFTP():New( "ftp.stest.info", oInternet,"user", "PW" )
   local aFiles
   LOCAL handle
   local cFile     := ""

   if ! Empty( oFTP:hFTP )

   FtpSetCurrentDirectory(oFTP:hFTP,  "/testinfo/STB1/")
   aFiles = oFTP:Directory("*.*" )

   IF len(aFiles) > 0

      AEval( aFiles, { | aFile | cFile := ALLTRIM( aFile[ 1 ] ),;
         cFile := STRTRAN(UPPER(cFile),".TXT",".LTX"),;
         memowrit( "c:\ITOUCH\prnsvr\dbf\" + cFile, ("STBWEB  "+DTOC(date())+" / "+time())  ) ,;
         FtpGetFile (oFTP:hFTP,aFile[ 1 ], "c:\ITOUCH\prnsvr\dbf\" + aFile[ 1 ] ,.t.,0,0,0 ) ,;
         oFTP:DeleteFile( aFile[ 1 ]),;
         ferase ("c:\ITOUCH\prnsvr\dbf\" + cFile)   } )

   ENDIF


else
   MsgAlert( "oFTP:hFTP is null" )
endif

oInternet:End()


return nil

//----------------------------------------------------------------------------//
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Post by Antonio Linares »

Jeff,

Please review FWH\samples\ftpdir.prg

You can easily modify it to check for a file.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Jeff Barnes
Posts: 933
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada
Contact:

Post by Jeff Barnes »

Thanks Antonio/Otto,

I have modified some code to allow me to upload/download a single file and also to check/delete a file.

If anyone is interested here it is:

Code: Select all | Expand

FUNCTION FtpSendFile( cFTPSite, cSource, cTarget, cUser, cPass, nBufSize )
   LOCAL oInternet, oFTP

   DEFAULT cUser:="", cPass:="", nBufSize:=2000
   IF EMPTY(cFtpSite) .or. EMPTY(cSource) .or. EMPTY(cTarget)
      MsgInfo("Missing Parameters"+chr(13)+"Usage: FtpSendFile(cFtpSite, cSource, cTarget, cUser, cPass, nBufSize)  ","FtpSendFile()")
      Return .f.
   ENDIF

    oInternet := TInternet():New()
    IF Empty( oInternet:hSession )
       MsgAlert( "Internet session not available!" )
    ELSE
       oFTP := TFTP():New( cFTPSite, oInternet, cUser, cPass )
       IF Empty( oFTP:hFTP )
          MsgStop( "Cannot connect to "+cFtpSite )
          oInternet:End()
          return .f.
       ENDIF
    ENDIF

    SendFiles( cSource, cTarget, nBufSize, oFTP )
   
    oInternet:End()

return nil

//----------------------------------------------------------------------------//

static function SendFiles( cSource, cTarget, nBufSize, oFTP )
   local hSource
   local cBuffer := Space( nBufSize )
   local nBytes
   local oFile

   if ! File( cSource )
      MsgStop( "File not found: " + cSource )
      Return .f.
   endif

   hSource = FOpen( cSource )

   oFile = TFtpFile():New( cTarget, oFTP )
   oFile:OpenWrite()
   FSeek( hSource, 0, 0 )
   nFile := 0

   while ( nBytes := FRead( hSource, @cBuffer, nBufSize ) ) > 0
      oFile:Write( SubStr( cBuffer, 1, nBytes ) )
   end
   FClose( hSource )
   oFile:End()
return .t.

//----------------------------------------------------------------------------//

function FTPGetFile( cFTPSite, cSource, cTarget, cUser, cPass, nBufSize )
   local oInternet, oFTP
   
   DEFAULT cUser:="", cPass:="", nBufSize:=2000
   IF EMPTY(cFtpSite) .or. EMPTY(cSource) .or. EMPTY(cTarget)
      MsgInfo("Missing Parameters"+chr(13)+"Usage: FtpGetFile(cFtpSite, cSource, cTarget, cUser, cPass, nBufSize)  ","FtpSendFile()")
      Return .f.
   ENDIF
   
   oInternet := TInternet():New()
   IF Empty( oInternet:hSession )
      MsgAlert( "Internet session not available!" )
      oInternet:End()
      return .f.
   ELSE
      oFTP := TFTP():New( cFTPSite, oInternet, cUser, cPass )
      if Empty( oFTP:hFTP )
         MsgStop( "Cannot connect to "+cFtpSite)
         oInternet:End()
         return .f.
      endif
   ENDIF

   GetFiles( cSource, cTarget, nBufSize, oFTP )

   oInternet:End()

return .t.

//----------------------------------------------------------------------------//

static function GetFiles( cSource, cTarget, nBufSize, oFTP )
   local hTarget
   local cBuffer := Space( nBufSize )
   local nBytes
   local oFile, aFiles

   hTarget = FCreate( cTarget )
   oFile = TFtpFile():New( cSource, oFTP )
   oFile:OpenRead()

   while ( nBytes := Len( cBuffer := oFile:Read( nBufSize ) ) ) > 0
      FWrite( hTarget, cBuffer, nBytes )
   end
   FClose( hTarget )
   oFile:End()
return nil

//----------------------------------------------------------------------------//

function FtpIsFile(cFtpSite, cFile, cUser, cPass,  lDelete)
   local oInternet := TInternet():New()
   local oFTP      := TFTP():New( cFtpSite, oInternet, cUser, cPass )
   local aFiles, lFound:=.f.

   DEFAULT cUser := "", cPass:="", lDelete:=.f.
   IF EMPTY(cFtpSite) .or. EMPTY(cFile)
      Msginfo("Missing Parameters"+chr(13)+"Usage: CheckFtpFile(cFtpSite, cFile, cUser, cPass, lDelete)  ","CheckFtpFile()")
      Return .f.
   ENDIF
   
   IF ! Empty( oFTP:hFTP )
      aFiles = oFTP:Directory( "*.*" )
      AEval( aFiles, { | aFile | IIF( aFile[ 1 ] = cFile,(lFound:=lDeleted:=.t.), ) } )
   ENDIF
   
   if lDelete
      if lFound
         oFtp:deletefile(cFile)
         msginfo("deleteed")
      endif
   endif

   oInternet:End()
return lFound
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Antonio Linares
Site Admin
Posts: 42513
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 73 times
Contact:

Post by Antonio Linares »

Jeff,

many thanks! :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply