Ftp uploading files

Ftp uploading files

Postby wartiaga » Wed Mar 29, 2023 2:00 pm

Hi,

Is anyone using any ftp classes that actually work? I tested some examples here on the forum and it even connects but it sends the file with zero size.

Thanks in advance.
wartiaga
 
Posts: 211
Joined: Wed May 25, 2016 1:04 am

Re: Ftp uploading files

Postby alerchster » Wed Mar 29, 2023 3:01 pm

Code: Select all  Expand view
#include "fivewin.ch"

// File Attributes
#define FILE_ATTRIBUTE_READONLY                         1
#define FILE_ATTRIBUTE_HIDDEN                           2
#define FILE_ATTRIBUTE_SYSTEM                           4
#define FILE_ATTRIBUTE_DIRECTORY                       16
#define FILE_ATTRIBUTE_ARCHIVE                         32
#define FILE_ATTRIBUTE_NORMAL                         128
#define FILE_ATTRIBUTE_TEMPORARY                      256

// Access Types for InternetOpen()
#define INTERNET_OPEN_TYPE_PRECONFIG                    0   // use registry configuration
#define INTERNET_OPEN_TYPE_DIRECT                       1   // direct to net
#define INTERNET_OPEN_TYPE_PROXY                        3   // via named proxy
#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY  4   // prevent using java/script/INS

// Manifests
#define INTERNET_INVALID_PORT_NUMBER                    0   // use the protocol-specific default
#define INTERNET_DEFAULT_FTP_PORT                      21   // default for FTP servers
#define INTERNET_DEFAULT_GOPHER_PORT                   70   //    "     " gopher "
#define INTERNET_DEFAULT_HTTP_PORT                     80   //    "     " HTTP   "
#define INTERNET_DEFAULT_HTTPS_PORT                   443   //    "     " HTTPS  "
#define INTERNET_DEFAULT_SOCKS_PORT                  1080   // default for SOCKS firewall servers.

// Service Types for InternetConnect()
#define INTERNET_SERVICE_FTP                            1
#define INTERNET_SERVICE_GOPHER                         2
#define INTERNET_SERVICE_HTTP                           3

// Flags for FTP
#define INTERNET_FLAG_TRANSFER_ASCII                    1
#define INTERNET_FLAG_TRANSFER_BINARY                   2

// File Access Types
#define GENERIC_READ                           2147483648
#define GENERIC_WRITE                          1073741824

STATIC cFtpSite, cFtpUserName, cFtpPassword, cDestination

FUNCTION Main()

LOCAL lOk := .F.

cFtpSite     := "....."
cFtpUserName := "....."
cFtpPassword := "....."
cDestination := "......"

* 1 Upload
lOk := UpLoadDownLoad(1, {"updwn.prg"}, cDestination, "UPLDDNLD.PRG")

* 2 Download
UpLoadDownLoad(2, {cDestination+"/UPLDDNLD.PRG"}, "C:\FWH\SAMPLES\", "UPLDDNLD.PRG")

RETURN nil

*******************************************************************************
*** FUNCTION UpLoadDownLoad(nOpt) - Upload / Download Data to/from FTP Site ***
*******************************************************************************

FUNCTION UpLoadDownLoad(nUpDown, aDataFiles, cDestination, cDataName, lAuto)

LOCAL oBrush, oDlg, oGroup, oGroup1, oGroup2
LOCAL oMeter1, oMeter2, nMeter1, nMeter2, cTitle
LOCAL aUpLoadFiles := {}, aDownLoadFiles := {}
LOCAL bAction, lOk := .F., lIsInternet := .F.
LOCAL aGradiate := { { .25, nRgb( 152,194,152 ), CLR_WHITE }, { .75,CLR_WHITE, nRgb( 152,194,152 ) } }

DEFAULT lAuto := .F.

MsgInfo("
Trying to connect to Internet. Please wait...")

lIsInternet := IsInterNet()

IF !lIsInternet
   RETURN .F.
ENDIF

IF nUpDown = 1
   IF !lAuto
      aUpLoadFiles   := ACLONE(aDataFiles)
      cTitle         := "
Uploading Data. Please wait..."
      bAction        := {||lOk := FtpUpLoad(  cFtpSite,       ;
                                              cFtpUsername,   ;
                                              cFtpPassword,   ;
                                              oMeter1,        ;
                                              oMeter2,        ;
                                              aUpLoadFiles,   ;
                                              cDestination,   ;
                                              cDataName,      ;
                                              lAuto, oDlg)}
   ELSE

      aUpLoadFiles   := ACLONE(aDataFiles)
      lOk := FtpUpLoad( cFtpSite, cFtpUsername, cFtpPassword, ;
                        oMeter1, oMeter2, aUpLoadFiles,       ;
                        cDestination, cDataName, lAuto)
  ENDIF
ELSE
   IF !lAuto
      aDownLoadFiles := ACLONE(aDataFiles)
      cTitle         := "
Downloading Data. Please wait..."
      bAction        := {||lOk := FtpDownLoad(cFtpSite,       ;
                                              cFtpUsername,   ;
                                              cFtpPassword,   ;
                                              oMeter1,        ;
                                              oMeter2,        ;
                                              aDownLoadFiles, ;
                                              cDestination,   ;
                                              cDataName,      ;
                                              lAuto,oDlg)}
  ELSE
      lOk := FtpDownLoad(cFtpSite, cFtpUsername, cFtpPassword,;
                         oMeter1, oMeter2, aDownLoadFiles,    ;
                         cDestination, cDataName, lAuto, oDlg)
  ENDIF
ENDIF

IF !lAuto

   DEFINE BRUSH oBrush GRADIENT aGradiate

   DEFINE DIALOG oDlg RESOURCE "
CONNECT"                     ;
          COLORS CLR_BLACK, nRGB(250,239,247)                ;
          TRANSPARENT BRUSH oBrush

   oDlg:cTitle := cTitle

   REDEFINE GROUP   oGroup  ID 301 OF oDlg TRANSPARENT COLOR CLR_HRED

   REDEFINE GROUP   oGroup1 ID 302 OF oDlg TRANSPARENT COLOR CLR_HBLUE
   REDEFINE METEREX oMeter1 VAR nMeter1 ID 101 OF oDlg

   REDEFINE GROUP   oGroup2 ID 303 OF oDlg TRANSPARENT COLOR CLR_HBLUE
   REDEFINE METEREX oMeter2 VAR nMeter2 ID 102 OF oDlg                               ;
            GRADIENT CHUNK { { 1/2, nRGB( 255, 251, 229 ), nRGB( 250, 223, 143 ) }  ,;
                             { 1/2, nRGB( 244, 194,  51 ), nRGB( 252, 235, 173 ) } } ;
            GRADIENT TRACK { { 1/2, nRGB( 198, 203, 213 ), nRGB( 219, 224, 233 ) }  ,;
                             { 1/2, nRGB( 224, 238, 237 ), nRGB( 224, 238, 237 ) } } ;

   * This block gets evaluated only the first time the DialogBox is painted !!!
   oDlg:bStart := {||(Eval( bAction), SysWait(1), oDlg:End())}

   ACTIVATE DIALOG oDlg CENTERED

   RELEASE BRUSH oBrush

   RETURN lOk

ENDIF

RETURN lOk

*******************************************************************************
*** STATIC FUNCTION UpLoad(oMeter, cSourceFile, cDestination)               ***
*******************************************************************************

STATIC FUNCTION FTPUpLoad(cFtpSite, cUsername, cPassword, oMeter1, oMeter2,   ;
                          aSourceFiles, cDestination, cDataName, lAuto,       ;
                          oDlg)

LOCAL hInternet, hConnect, hSource, hDest, nRead
LOCAL cSourceFile, cData := SPACE( 32768 ) //cData := SPACE( 1024 )
LOCAL nPos  := 0, n, lSucess := .T., nTotal := 0

hInternet   := InternetOpen( "
Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 )

hConnect    := InternetConnect( hInternet, cFtpSite, INTERNET_INVALID_PORT_NUMBER,;
                                cUsername, cPassword, INTERNET_SERVICE_FTP, 0, 0 )

IF !lAuto
   oMeter1:Set( 0 )
   oMeter1:nTotal := LEN(aSourceFiles)
ENDIF

FOR n = 1 TO LEN(aSourceFiles)

    cSourceFile := SUBSTR(aSourceFiles[n],RAT("
\",aSourceFiles[n])+1)
    hDest       := FtpOpenFile( hConnect, cDestination+cDataName, GENERIC_WRITE, 0, 0 )

    nTotal := FSize(aSourceFiles[n])

    IF !lAuto
       oMeter2:Set( 0 )
       oMeter2:nTotal := nTotal
    ENDIF

    hSource := FOpen(aSourceFiles[n])

    WHILE .T.

      nRead := FRead( hSource, @cData, LEN( cData ) )

      IF nRead = 0
         IF FERROR() # 0
            lSucess := .F.
         ENDIF
         EXIT
      ENDIF
      IF !InternetWriteFile( hDest, @cData, nRead )
         lSucess := .F.
         EXIT
      ELSE
         nPos += LEN( cData )
         IF !lAuto
            oMeter2:Set( nPos )
         ENDIF
      ENDIF

    ENDDO

    IF !lAuto
       oMeter1:Set( n )
    ENDIF

    FClose( hSource )
    InternetCloseHandle( hDest )

    SysRefresh()

    IF .NOT. lSucess
       EXIT
    ENDIF

NEXT

InternetCloseHandle( hConnect )
InternetCloseHandle( hInternet )

SysRefresh()

IF .NOT. lAuto
   IF lSucess
      MsgInfo(cDataName+"
has been Uploaded Successfully.")
   ELSE
      MsgInfo("
Error in Uploading "+cDataName+".")
   ENDIF
ENDIF

IF !lAuto
   oMeter2:Set( 0 )
   oMeter1:Set( 0 )
   oDlg:End()
ENDIF

RETURN lSucess

*******************************************************************************
*** STATIC FUNCTION DownLoad( oMeter, cSourceFile, cDestFile )              ***
*******************************************************************************

STATIC FUNCTION FTPDownLoad(cFtpSite, cUsername, cPassword, oMeter1, oMeter2, ;
                            aSourceFiles, cDestFile, cDataName, lAuto,        ;
                            oDlg)

LOCAL hInternet, hConnect, hSource, hDest, nRead
LOCAL cSourceFile, cData := SPACE( 32768 ) //cData := SPACE( 1024 )

LOCAL nPos  := 0, n, lSucess := .T.

hInternet   := InternetOpen( "
Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 )
hConnect    := InternetConnect( hInternet, cFtpSite, INTERNET_INVALID_PORT_NUMBER,;
                                cUsername, cPassword, INTERNET_SERVICE_FTP, 0, 0 )
IF !lAuto
   oMeter1:Set( 0 )
   oMeter1:nTotal := LEN(aSourceFiles)
ENDIF

FOR n = 1 TO LEN(aSourceFiles)

    cSourceFile := aSourceFiles[n]
    hSource     := FtpOpenFile( hConnect, cSourceFile, GENERIC_READ, 0, 0 )

    IF !lAuto
       oMeter2:Set( 0 )
       oMeter2:nTotal := FtpGetFileSize( hSource )
    ENDIF

    hDest := FCreate( cDestFile+SUBSTR(cSourceFile,RAT("
/",cSourceFile)+1) )

    WHILE .T.

      nRead := InternetReadFile( hSource, @cData )

      IF nRead = -1
         lSucess := .F.
         EXIT
      ENDIF

      IF nRead = 0
         EXIT
      ENDIF

      FWRITE( hDest, cData, nRead )

      nPos += LEN(ALLTRIM(cData))

      IF !lAuto
         oMeter2:Set( nPos )
      ENDIF

    ENDDO

    FClose( hDest )
    InternetCloseHandle( hSource )
    SysRefresh()

    IF !lAuto
       oMeter1:Set( n )
    ENDIF

    IF .NOT. lSucess
       EXIT
    ENDIF

NEXT

InternetCloseHandle( hConnect )
InternetCloseHandle( hInternet )

SysRefresh()

IF .NOT. lAuto
   IF lSucess
      MsgInfo(cDataName+"
has been Downloaded Successfully.")
   ELSE
      MsgInfo("
Error in Downloading "+cDataName+".")
   ENDIF
ENDIF

IF !lAuto
   oMeter2:Set( 0 )
   oMeter1:Set( 0 )
   oDlg:End()
ENDIF

RETURN lSucess

*******************************************************************************
*** FTP Upload/Download FUNCTION Wrappers of WININET.LIB                    ***
*******************************************************************************

#pragma BEGINDUMP

#include "
windows.h"
#include "
wininet.h"
#include "
hbapi.h"

HB_FUNC( INTERNETOPEN )
{
    hb_retnl( ( LONG ) InternetOpen( hb_parc( 1 ), hb_parnl( 2 ), hb_parc( 3 ), hb_parc( 4 ), hb_parnl( 5 ) ) );
}

//***********************

HB_FUNC( INTERNETCLOSEHANDLE )
{
    hb_retl( InternetCloseHandle( ( HINTERNET ) hb_parnl( 1 ) ) );
}

//***********************

HB_FUNC( INTERNETCONNECT )
{
    hb_retnl( ( LONG ) InternetConnect( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), ( INTERNET_PORT ) hb_parnl( 3 ), hb_parc( 4 ), hb_parc( 5 ), hb_parnl( 6 ), hb_parnl( 7 ), hb_parnl( 8 ) ) );
}

//***********************

HB_FUNC( FTPOPENFILE )
{
    hb_retnl( ( LONG ) FtpOpenFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parnl( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}

//***********************

HB_FUNC( FTPGETFILESIZE )
{
    DWORD nFileSizeHigh;

    hb_retnl( ( LONG ) FtpGetFileSize( ( HINTERNET ) hb_parnl( 1 ), &nFileSizeHigh ) );
}

//***********************

HB_FUNC( INTERNETREADFILE )
{
    DWORD nBytesRead;

    BOOL lSuccess = InternetReadFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parclen( 2 ), &nBytesRead );

    if ( !lSuccess )
        hb_retnl( -1 );
    else
        hb_retnl( nBytesRead );
}

//***********************

HB_FUNC( INTERNETWRITEFILE )
{
    DWORD nBytesWritten;

    BOOL lSuccess = InternetWriteFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parnl( 3 ), &nBytesWritten );

    hb_retl( lSuccess );
}


HB_FUNC( FTPGETFILE )
{
    hb_retl( FtpGetFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parc( 3 ), hb_parl( 4 ), hb_parnl( 5 ), hb_parnl( 6 ), hb_parnl( 7 ) ) );
}

HB_FUNC( FTPPUTFILE )
{
    hb_retl( FtpPutFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parc( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}

HB_FUNC( FTPDELETEFILE )
{
    hb_retl( FtpDeleteFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}

HB_FUNC( FTPCREATEDIRECTORY )
{
    hb_retl( FtpCreateDirectory( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}

HB_FUNC( FTPREMOVEDIRECTORY )
{
    hb_retl( FtpRemoveDirectory( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}

HB_FUNC( FTPFINDFIRSTFILE )
{
    hb_retnl( ( LONG ) FtpFindFirstFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), ( WIN32_FIND_DATA * ) hb_parc( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}

HB_FUNC( INTERNETFINDNEXTFILE )
{
    hb_retl( InternetFindNextFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}

#pragma ENDDUMP
Regards

Ing. Anton Lerchster
User avatar
alerchster
 
Posts: 92
Joined: Mon Oct 22, 2012 4:43 pm

Re: Ftp uploading files

Postby wartiaga » Wed Mar 29, 2023 3:27 pm

alerchster wrote:
Code: Select all  Expand view
#include "fivewin.ch"

// File Attributes
#define FILE_ATTRIBUTE_READONLY                         1
#define FILE_ATTRIBUTE_HIDDEN                           2
#define FILE_ATTRIBUTE_SYSTEM                           4
#define FILE_ATTRIBUTE_DIRECTORY                       16
#define FILE_ATTRIBUTE_ARCHIVE                         32
#define FILE_ATTRIBUTE_NORMAL                         128
#define FILE_ATTRIBUTE_TEMPORARY                      256

// Access Types for InternetOpen()
#define INTERNET_OPEN_TYPE_PRECONFIG                    0   // use registry configuration
#define INTERNET_OPEN_TYPE_DIRECT                       1   // direct to net
#define INTERNET_OPEN_TYPE_PROXY                        3   // via named proxy
#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY  4   // prevent using java/script/INS

// Manifests
#define INTERNET_INVALID_PORT_NUMBER                    0   // use the protocol-specific default
#define INTERNET_DEFAULT_FTP_PORT                      21   // default for FTP servers
#define INTERNET_DEFAULT_GOPHER_PORT                   70   //    "     " gopher "
#define INTERNET_DEFAULT_HTTP_PORT                     80   //    "     " HTTP   "
#define INTERNET_DEFAULT_HTTPS_PORT                   443   //    "     " HTTPS  "
#define INTERNET_DEFAULT_SOCKS_PORT                  1080   // default for SOCKS firewall servers.

// Service Types for InternetConnect()
#define INTERNET_SERVICE_FTP                            1
#define INTERNET_SERVICE_GOPHER                         2
#define INTERNET_SERVICE_HTTP                           3

// Flags for FTP
#define INTERNET_FLAG_TRANSFER_ASCII                    1
#define INTERNET_FLAG_TRANSFER_BINARY                   2

// File Access Types
#define GENERIC_READ                           2147483648
#define GENERIC_WRITE                          1073741824

STATIC cFtpSite, cFtpUserName, cFtpPassword, cDestination

FUNCTION Main()

LOCAL lOk := .F.

cFtpSite     := "....."
cFtpUserName := "....."
cFtpPassword := "....."
cDestination := "......"

* 1 Upload
lOk := UpLoadDownLoad(1, {"updwn.prg"}, cDestination, "UPLDDNLD.PRG")

* 2 Download
UpLoadDownLoad(2, {cDestination+"/UPLDDNLD.PRG"}, "C:\FWH\SAMPLES\", "UPLDDNLD.PRG")

RETURN nil

*******************************************************************************
*** FUNCTION UpLoadDownLoad(nOpt) - Upload / Download Data to/from FTP Site ***
*******************************************************************************

FUNCTION UpLoadDownLoad(nUpDown, aDataFiles, cDestination, cDataName, lAuto)

LOCAL oBrush, oDlg, oGroup, oGroup1, oGroup2
LOCAL oMeter1, oMeter2, nMeter1, nMeter2, cTitle
LOCAL aUpLoadFiles := {}, aDownLoadFiles := {}
LOCAL bAction, lOk := .F., lIsInternet := .F.
LOCAL aGradiate := { { .25, nRgb( 152,194,152 ), CLR_WHITE }, { .75,CLR_WHITE, nRgb( 152,194,152 ) } }

DEFAULT lAuto := .F.

MsgInfo("
Trying to connect to Internet. Please wait...")

lIsInternet := IsInterNet()

IF !lIsInternet
   RETURN .F.
ENDIF

IF nUpDown = 1
   IF !lAuto
      aUpLoadFiles   := ACLONE(aDataFiles)
      cTitle         := "
Uploading Data. Please wait..."
      bAction        := {||lOk := FtpUpLoad(  cFtpSite,       ;
                                              cFtpUsername,   ;
                                              cFtpPassword,   ;
                                              oMeter1,        ;
                                              oMeter2,        ;
                                              aUpLoadFiles,   ;
                                              cDestination,   ;
                                              cDataName,      ;
                                              lAuto, oDlg)}
   ELSE

      aUpLoadFiles   := ACLONE(aDataFiles)
      lOk := FtpUpLoad( cFtpSite, cFtpUsername, cFtpPassword, ;
                        oMeter1, oMeter2, aUpLoadFiles,       ;
                        cDestination, cDataName, lAuto)
  ENDIF
ELSE
   IF !lAuto
      aDownLoadFiles := ACLONE(aDataFiles)
      cTitle         := "
Downloading Data. Please wait..."
      bAction        := {||lOk := FtpDownLoad(cFtpSite,       ;
                                              cFtpUsername,   ;
                                              cFtpPassword,   ;
                                              oMeter1,        ;
                                              oMeter2,        ;
                                              aDownLoadFiles, ;
                                              cDestination,   ;
                                              cDataName,      ;
                                              lAuto,oDlg)}
  ELSE
      lOk := FtpDownLoad(cFtpSite, cFtpUsername, cFtpPassword,;
                         oMeter1, oMeter2, aDownLoadFiles,    ;
                         cDestination, cDataName, lAuto, oDlg)
  ENDIF
ENDIF

IF !lAuto

   DEFINE BRUSH oBrush GRADIENT aGradiate

   DEFINE DIALOG oDlg RESOURCE "
CONNECT"                     ;
          COLORS CLR_BLACK, nRGB(250,239,247)                ;
          TRANSPARENT BRUSH oBrush

   oDlg:cTitle := cTitle

   REDEFINE GROUP   oGroup  ID 301 OF oDlg TRANSPARENT COLOR CLR_HRED

   REDEFINE GROUP   oGroup1 ID 302 OF oDlg TRANSPARENT COLOR CLR_HBLUE
   REDEFINE METEREX oMeter1 VAR nMeter1 ID 101 OF oDlg

   REDEFINE GROUP   oGroup2 ID 303 OF oDlg TRANSPARENT COLOR CLR_HBLUE
   REDEFINE METEREX oMeter2 VAR nMeter2 ID 102 OF oDlg                               ;
            GRADIENT CHUNK { { 1/2, nRGB( 255, 251, 229 ), nRGB( 250, 223, 143 ) }  ,;
                             { 1/2, nRGB( 244, 194,  51 ), nRGB( 252, 235, 173 ) } } ;
            GRADIENT TRACK { { 1/2, nRGB( 198, 203, 213 ), nRGB( 219, 224, 233 ) }  ,;
                             { 1/2, nRGB( 224, 238, 237 ), nRGB( 224, 238, 237 ) } } ;

   * This block gets evaluated only the first time the DialogBox is painted !!!
   oDlg:bStart := {||(Eval( bAction), SysWait(1), oDlg:End())}

   ACTIVATE DIALOG oDlg CENTERED

   RELEASE BRUSH oBrush

   RETURN lOk

ENDIF

RETURN lOk

*******************************************************************************
*** STATIC FUNCTION UpLoad(oMeter, cSourceFile, cDestination)               ***
*******************************************************************************

STATIC FUNCTION FTPUpLoad(cFtpSite, cUsername, cPassword, oMeter1, oMeter2,   ;
                          aSourceFiles, cDestination, cDataName, lAuto,       ;
                          oDlg)

LOCAL hInternet, hConnect, hSource, hDest, nRead
LOCAL cSourceFile, cData := SPACE( 32768 ) //cData := SPACE( 1024 )
LOCAL nPos  := 0, n, lSucess := .T., nTotal := 0

hInternet   := InternetOpen( "
Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 )

hConnect    := InternetConnect( hInternet, cFtpSite, INTERNET_INVALID_PORT_NUMBER,;
                                cUsername, cPassword, INTERNET_SERVICE_FTP, 0, 0 )

IF !lAuto
   oMeter1:Set( 0 )
   oMeter1:nTotal := LEN(aSourceFiles)
ENDIF

FOR n = 1 TO LEN(aSourceFiles)

    cSourceFile := SUBSTR(aSourceFiles[n],RAT("
\",aSourceFiles[n])+1)
    hDest       := FtpOpenFile( hConnect, cDestination+cDataName, GENERIC_WRITE, 0, 0 )

    nTotal := FSize(aSourceFiles[n])

    IF !lAuto
       oMeter2:Set( 0 )
       oMeter2:nTotal := nTotal
    ENDIF

    hSource := FOpen(aSourceFiles[n])

    WHILE .T.

      nRead := FRead( hSource, @cData, LEN( cData ) )

      IF nRead = 0
         IF FERROR() # 0
            lSucess := .F.
         ENDIF
         EXIT
      ENDIF
      IF !InternetWriteFile( hDest, @cData, nRead )
         lSucess := .F.
         EXIT
      ELSE
         nPos += LEN( cData )
         IF !lAuto
            oMeter2:Set( nPos )
         ENDIF
      ENDIF

    ENDDO

    IF !lAuto
       oMeter1:Set( n )
    ENDIF

    FClose( hSource )
    InternetCloseHandle( hDest )

    SysRefresh()

    IF .NOT. lSucess
       EXIT
    ENDIF

NEXT

InternetCloseHandle( hConnect )
InternetCloseHandle( hInternet )

SysRefresh()

IF .NOT. lAuto
   IF lSucess
      MsgInfo(cDataName+"
has been Uploaded Successfully.")
   ELSE
      MsgInfo("
Error in Uploading "+cDataName+".")
   ENDIF
ENDIF

IF !lAuto
   oMeter2:Set( 0 )
   oMeter1:Set( 0 )
   oDlg:End()
ENDIF

RETURN lSucess

*******************************************************************************
*** STATIC FUNCTION DownLoad( oMeter, cSourceFile, cDestFile )              ***
*******************************************************************************

STATIC FUNCTION FTPDownLoad(cFtpSite, cUsername, cPassword, oMeter1, oMeter2, ;
                            aSourceFiles, cDestFile, cDataName, lAuto,        ;
                            oDlg)

LOCAL hInternet, hConnect, hSource, hDest, nRead
LOCAL cSourceFile, cData := SPACE( 32768 ) //cData := SPACE( 1024 )

LOCAL nPos  := 0, n, lSucess := .T.

hInternet   := InternetOpen( "
Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 )
hConnect    := InternetConnect( hInternet, cFtpSite, INTERNET_INVALID_PORT_NUMBER,;
                                cUsername, cPassword, INTERNET_SERVICE_FTP, 0, 0 )
IF !lAuto
   oMeter1:Set( 0 )
   oMeter1:nTotal := LEN(aSourceFiles)
ENDIF

FOR n = 1 TO LEN(aSourceFiles)

    cSourceFile := aSourceFiles[n]
    hSource     := FtpOpenFile( hConnect, cSourceFile, GENERIC_READ, 0, 0 )

    IF !lAuto
       oMeter2:Set( 0 )
       oMeter2:nTotal := FtpGetFileSize( hSource )
    ENDIF

    hDest := FCreate( cDestFile+SUBSTR(cSourceFile,RAT("
/",cSourceFile)+1) )

    WHILE .T.

      nRead := InternetReadFile( hSource, @cData )

      IF nRead = -1
         lSucess := .F.
         EXIT
      ENDIF

      IF nRead = 0
         EXIT
      ENDIF

      FWRITE( hDest, cData, nRead )

      nPos += LEN(ALLTRIM(cData))

      IF !lAuto
         oMeter2:Set( nPos )
      ENDIF

    ENDDO

    FClose( hDest )
    InternetCloseHandle( hSource )
    SysRefresh()

    IF !lAuto
       oMeter1:Set( n )
    ENDIF

    IF .NOT. lSucess
       EXIT
    ENDIF

NEXT

InternetCloseHandle( hConnect )
InternetCloseHandle( hInternet )

SysRefresh()

IF .NOT. lAuto
   IF lSucess
      MsgInfo(cDataName+"
has been Downloaded Successfully.")
   ELSE
      MsgInfo("
Error in Downloading "+cDataName+".")
   ENDIF
ENDIF

IF !lAuto
   oMeter2:Set( 0 )
   oMeter1:Set( 0 )
   oDlg:End()
ENDIF

RETURN lSucess

*******************************************************************************
*** FTP Upload/Download FUNCTION Wrappers of WININET.LIB                    ***
*******************************************************************************

#pragma BEGINDUMP

#include "
windows.h"
#include "
wininet.h"
#include "
hbapi.h"

HB_FUNC( INTERNETOPEN )
{
    hb_retnl( ( LONG ) InternetOpen( hb_parc( 1 ), hb_parnl( 2 ), hb_parc( 3 ), hb_parc( 4 ), hb_parnl( 5 ) ) );
}

//***********************

HB_FUNC( INTERNETCLOSEHANDLE )
{
    hb_retl( InternetCloseHandle( ( HINTERNET ) hb_parnl( 1 ) ) );
}

//***********************

HB_FUNC( INTERNETCONNECT )
{
    hb_retnl( ( LONG ) InternetConnect( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), ( INTERNET_PORT ) hb_parnl( 3 ), hb_parc( 4 ), hb_parc( 5 ), hb_parnl( 6 ), hb_parnl( 7 ), hb_parnl( 8 ) ) );
}

//***********************

HB_FUNC( FTPOPENFILE )
{
    hb_retnl( ( LONG ) FtpOpenFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parnl( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}

//***********************

HB_FUNC( FTPGETFILESIZE )
{
    DWORD nFileSizeHigh;

    hb_retnl( ( LONG ) FtpGetFileSize( ( HINTERNET ) hb_parnl( 1 ), &nFileSizeHigh ) );
}

//***********************

HB_FUNC( INTERNETREADFILE )
{
    DWORD nBytesRead;

    BOOL lSuccess = InternetReadFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parclen( 2 ), &nBytesRead );

    if ( !lSuccess )
        hb_retnl( -1 );
    else
        hb_retnl( nBytesRead );
}

//***********************

HB_FUNC( INTERNETWRITEFILE )
{
    DWORD nBytesWritten;

    BOOL lSuccess = InternetWriteFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parnl( 3 ), &nBytesWritten );

    hb_retl( lSuccess );
}


HB_FUNC( FTPGETFILE )
{
    hb_retl( FtpGetFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parc( 3 ), hb_parl( 4 ), hb_parnl( 5 ), hb_parnl( 6 ), hb_parnl( 7 ) ) );
}

HB_FUNC( FTPPUTFILE )
{
    hb_retl( FtpPutFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parc( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}

HB_FUNC( FTPDELETEFILE )
{
    hb_retl( FtpDeleteFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}

HB_FUNC( FTPCREATEDIRECTORY )
{
    hb_retl( FtpCreateDirectory( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}

HB_FUNC( FTPREMOVEDIRECTORY )
{
    hb_retl( FtpRemoveDirectory( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}

HB_FUNC( FTPFINDFIRSTFILE )
{
    hb_retnl( ( LONG ) FtpFindFirstFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), ( WIN32_FIND_DATA * ) hb_parc( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}

HB_FUNC( INTERNETFINDNEXTFILE )
{
    hb_retl( InternetFindNextFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}

#pragma ENDDUMP


Hello, thanks for the example. I can upload the file to ftp but it goes with size 0 when it should be 7,467 bytes
wartiaga
 
Posts: 211
Joined: Wed May 25, 2016 1:04 am

Re: Ftp uploading files

Postby karinha » Wed Mar 29, 2023 3:53 pm

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7808
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: Ftp uploading files

Postby wartiaga » Fri Mar 31, 2023 1:58 pm



Thanks Karinha, but none of the examples work.
wartiaga
 
Posts: 211
Joined: Wed May 25, 2016 1:04 am

Re: Ftp uploading files

Postby karinha » Fri Mar 31, 2023 2:10 pm

João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7808
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 82 guests