WriteFTPFile Method
Class: FTP_SERVICE
Description:Sends a file or string to the FTP server from some local area
This method sends a file or string to the FTP server from some local area. Depending on how the FTP Server is setup, decides whether or not an existing file is overwritten or if the function fails to send the file.
=
Object.WriteFTPFile
Parameter
lcRemoteFile
Remote file name
Type Character
Direction Input
Name and Path for the contents of the file to be stored in.
This parameter cannot be empty.
lcData
Data or file name
Type Character
Direction Input
Data or file name which will be send to FTP server.
This parameter cannot be empty.
liData
Buffer size
Type Integer
Direction Input
Optional  
Buffer size for writing to FTP server.
liStart
Start position
Type Integer
Direction Input
Optional  
Start position for resume mode.
liFlags
Data or file name is source
Type Integer
Direction Input
Optional  
ValueDescription
_FTPS_RWF_FilelcData must be file name.
_FTPS_RWF_StringlcData must be string.
_FTPS_RWF_ResumeResume mode, default.
lnFlag
The flags that indicate various options.
Type Numeric
Direction Input
Value is combined with FTP_SERVICE::nCachingType's value. If this parameter ommited, then default value is FTP_TRANSFER_TYPE_UNKNOWN. Can be one of the following:
ValueDescription
FTP_TRANSFER_TYPE_ASCIITransfers the file as ASCII.
FTP_TRANSFER_TYPE_BINARYTransfers the file as binary.
Assumed from MSDN.
Return value Boolean
The return value is .T. if file is uploaded or .F. is not.
Example
LOCAL loFTP,lcPom #INCLUDE "ftp.h" SET PROCEDURE TO ftp.prg ADDITIVE loFTP=CREATEOBJECT('_myftp') IF loFTP.OpenInternet("ABONNE", "PWD", "10.10.10.10", "21") * Write remote file into bottom from local file IF loFTP.WriteFtpFile("any.txt","local.txt",1000) ?STRTRAN("File %File% uploaded","%File%","local.txt") ELSE ?loFTP.GetExtendedErrorCode(),loFTP.GetExtendedErrorMsg() ENDIF * Write remote file into 10. byte (resume mode) from local file IF loFTP.WriteFtpFile("any.txt","local.txt",1000,10) ?STRTRAN("File %File% uploaded","%File%","local.txt") ELSE ?loFTP.GetExtendedErrorCode(),loFTP.GetExtendedErrorMsg() ENDIF * Write remote file into bottom from string lcPom=FILETOSTR("local.txt") IF loFTP.WriteFtpFile("any.txt",@lcPom,1000,0,_FTPS_RWF_String) ?STRTRAN("File %File% uploaded","%File%","local.txt") ELSE ?loFTP.GetExtendedErrorCode(),loFTP.GetExtendedErrorMsg() ENDIF * Write remote file into 10. byte from string lcPom=FILETOSTR("local.txt") IF loFTP.WriteFtpFile("any.txt",@lcPom,1000,10,_FTPS_RWF_String) ?STRTRAN("File %File% uploaded","%File%","local.txt") ELSE ?loFTP.GetExtendedErrorCode(),loFTP.GetExtendedErrorMsg() ENDIF =loFTP.CloseInternet() ENDIF RELEASE PROCEDURE ftp.prg DEFINE CLASS _myFTP AS FTP_SERVICE PROCEDURE BeforeWriteFTPFile(lcRemoteFile, lcData,liData,liStart,liFlags,lnFlag) ?PROGRAM(16) ?CHR(9),lcRemoteFile, liData,liStart,liFlags ENDPROC PROCEDURE AfterWriteFTPFile(lcRemoteFile, lcData,liData,liStart,liFlags,lnFlag,fResult) ?PROGRAM(16) ?CHR(9),lcRemoteFile, liData,liStart,liFlags,fResult ENDPROC PROCEDURE AtWriteFTPFile(lcRemoteFile, lcData,liData,liStart,liFlags,lnFlag,lii,liWrite,fResult) ?PROGRAM(16) ?CHR(9),lcRemoteFile, lcData,liData,liStart,liFlags,lii,liWrite,fResult ENDPROC ENDDEFINE
See also
Expand/Collapse source code of procedure WriteFTPFile Source Code
      LPARAMETERS INP lcRemoteFile, INP lcData,OPT_INP liData,OPT_INP liStart,OPT_INP liFlags,OPT_INP lnFlag
      LOCAL fResult,lihFTP,lcBuffer,liData,liWrite,lihFile,lcAll,lcAlias,liSele,llErr,lcError
      LOCAL ARRAY laFile(1)
      lihFile=0

      liData=IIF(PCOUNT()<3,512,m.liData)
      liData=IIF(liData<=0,512,m.liData)
      
      liStart=IIF(PCOUNT()<4,0,m.liStart)
      liFlags=IIF(PCOUNT()<5,_FTPS_RWF_File,m.liFlags)
      lnFlag=IIF(PCOUNT()<6,FTP_TRANSFER_TYPE_UNKNOWN,m.lnFlag)
      liSele=SELECT()
      lcAlias=SYS(2015)

      IF !BITTEST(m.liFlags,0) && File?
         IF ADIR(m.laFile,lcData,"RSHA")=0 && safety than FILE()
            This.SetExtendedError(_FTPS_UE_FNEXISTS,m.lcData)
            RETURN .F.
         ENDIF

         CREATE CURSOR (m.lcAlias) (XX000 M NOCPTRANS)
         APPEND BLANK
         #IF VAL(STRTRAN(SUBS(VERSION(),LEN("Visual FoxPro ")+1,2),"0",""))>=8
          TRY
           APPEND MEMO XX000 FROM (m.lcData)
          CATCH
           llErr=.T.
          FINALLY
          ENDTRY
         #ELSE
           lcError=ON("ERROR") 
           ON ERROR llErr=.T.
           APPEND MEMO XX000 FROM (m.lcData)
         #ENDIF
         IF llErr && bug at reading file from disk
            This.SetExtendedError(_FTPS_UE_CNF,lcData)
            RETURN .F.
         ENDIF
      ELSE
         IF TYPE("m.lcData")#"C"
            This.SetExtendedError(11,"Function argument value, type, or count is invalid.")
            RETURN .F.
         ENDIF
*         lcData=SUBST(lcData,liStart+1)
      ENDIF

      IF This.OpenFTPConnection(This.cCurrentDir)    && Open an FTP Handle
         lcRemoteFile = m.lcRemoteFile + cNULL

         =This.BeforeWriteFTPFile(@m.lcRemoteFile, @m.lcData,m.liData,m.liStart,m.liFlags,m.lnFlag)

         IF m.liStart>0 && Part data
            * Append file
            =This._FTPCommand("REST "+LTRIM(STR(m.liStart,11)), m.lnFlag,0,.NULL.)
            fResult=IIF(This._FTPCommand("APPE "+m.lcRemoteFile, m.lnFlag,0,@m.lihFTP),1,0)
         ENDIF

         STORE FtpOpenFile(This.nConnect_Handle, @m.lcRemoteFile, GENERIC_WRITE, ;
                           BITOR(This.nCachingType,m.lnFlag), 0) TO fResult,lihFTP
         =This.GetExtendedError()

         IF m.fResult #0 && OK, FTP file is openned
            DO CASE
               CASE !BITTEST(m.liFlags,0) AND m.liStart>=LEN(XX000) OR;
                     BITTEST(m.liFlags,0) AND LEN(m.lcData)=0 && prázdný soubor nebo prázdná data
                    lcBuffer=""
                    liData=LEN(m.lcBuffer)

                    liWrite=0
                    fResult = InternetWriteFile(m.lihFTP, @m.lcBuffer, m.liData, @m.liWrite)
                    =This.GetExtendedError()
                    =This.AtWriteFTPFile(@m.lcRemoteFile, @m.lcData,m.liData,m.liStart,m.liFlags,m.lnFlag,0,m.liWrite,m.fResult)
         
               CASE !BITTEST(m.liFlags,0) && File from string
                    lii=m.liStart+1
               
                    DO WHILE m.lii<LEN(XX000)
                       lcBuffer=SUBST(XX000,m.lii,m.liData)
                       liData=LEN(m.lcBuffer)

                       liWrite=0
                       fResult = InternetWriteFile(m.lihFTP, @m.lcBuffer, m.liData, @m.liWrite)
                       =This.GetExtendedError()
                       =This.AtWriteFTPFile(@m.lcRemoteFile, @m.lcData,m.liData,m.liStart,m.liFlags,m.lnFlag,m.lii,m.liWrite,m.fResult)
                       IF m.fResult#1
                          EXIT
                       ENDIF
                       lii=m.lii+m.liData
                    ENDDO

               CASE BITTEST(m.liFlags,0) && String data
                    lii=m.liStart+1
               
                    DO WHILE m.lii<LEN(m.lcData)
                       lcBuffer=SUBST(m.lcData,m.lii,m.liData)
                       liData=LEN(m.lcBuffer)

                       liWrite=0
                       fResult = InternetWriteFile(m.lihFTP, @m.lcBuffer, m.liData, @m.liWrite)
                       =This.GetExtendedError()
                       =This.AtWriteFTPFile(@m.lcRemoteFile, @m.lcData,m.liData,m.liStart,m.liFlags,m.lnFlag,m.lii,m.liWrite,m.fResult)
                       IF m.fResult#1
                          EXIT
                       ENDIF
                       lii=m.lii+m.liData
                    ENDDO

            ENDCASE

            =IIF(m.liStart>0,This._FTPCommand("NOOP", FTP_TRANSFER_TYPE_BINARY,0,.NULL.),.T.)
            =InternetCloseHandle(m.lihFTP)
         ENDIF
         IF USED(m.lcAlias)
            USE IN (m.lcAlias)
         ENDIF
         SELECT (m.liSele)

         =This.AfterWriteFTPFile(@m.lcRemoteFile, @m.lcData,m.liData,m.liStart,m.liFlags,m.lnFlag,m.fResult)
         =IIF(This.lMultiOperations,.T.,This.CloseFTPConnection())   && Close FTP Handle
         
         RETURN m.fResult = 1
      ENDIF
      RETURN .F.