Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Wwftp - ftpsendfile()
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
West Wind Web Connection
Divers
Thread ID:
00877680
Message ID:
00877843
Vues:
9
Hi,
There are some "weird bug" in wininet.dll. It returns error code even packet can be sent/downloaded successfully. It only happend for dial-up connection.

I have modified the code of FTPSendFileEx() and FTPGetFileEx() and it works fine for me.

Here the code:
wwFTP.FTPGetFileEx()
LPARAMETER lcSourceFile, lcTargetFile
LOCAL lhFileHandle, hFTPFile, tcBuffer, tnSize, tnBufferSize, lnRetVal, lnBytesRead, lnBufferReads
LOCAL ARRAY	laFiles[1]

DECLARE INTEGER FtpOpenFile ;
   IN WININET.DLL ;
   INTEGER hIPSession,;
   STRING @lpszFileName,;
   INTEGER dwAcessFlags,;
   INTEGER dwServiceFlags,;
   INTEGER dwContext


DECLARE INTEGER InternetReadFile ;
   IN WININET.DLL ;
   INTEGER hFTPHandle,;
   STRING lcBuffer,;
   INTEGER cbBuffer,;
   INTEGER @cbBuffer

***Get Download File info such as File size
IF THIS.aFTPDir(@laFiles, lcSourceFile) = 0
	RETURN THIS.nError
ENDIF

lnFileSize = laFiles[1, 2]

hFTPFile = FtpOpenFile(THIS.hFTPSession,lcSourceFile,;
                       GENERIC_READ,;
                       INTERNET_FLAG_RELOAD + FTP_TRANSFER_TYPE_BINARY,0)

IF hFTPFile = 0
   THIS.nError = GetLastError()
   THIS.cErrorMsg = THIS.GetSystemErrorMsg()
   RETURN THIS.nError
ENDIF

*** Build the buffer dynamically
THIS.lCancelDownload = .f.
tcBuffer = ""
tnSize = 0
tnBufferSize = 0
lnRetVal = 0
lnBytesRead = 1
lnBufferReads = 0
lhFileHandle = FCREATE(lcTargetFile)
IF lhFileHandle=-1
	  THIS.nError = FError()
      THIS.cErrorMsg = "Output file couldn't be created."
      InternetCloseHandle(hFTPFile)
      RETURN THIS.nError
ENDIF

DO WHILE .t.
     lcReadBuffer = SPACE(THIS.nFTPWorkBufferSize)
     lnBytesRead = 0
     lnSize = LEN(lcReadBuffer)

     lnRetval=InternetReadFile(hFTPFile,;
		   @lcReadBuffer,;
		   lnSize,;
		   @lnBytesRead)

		IF lnRetVal = 1 AND lnBytesRead > 0
		   *** Update the input parameters - result buffer and size of buffer
		   *tcBuffer = tcBuffer + LEFT(lcReadBuffer,lnBytesRead)
		   =FWRITE(lhFileHandle,LEFT(lcReadBuffer,lnBytesRead) )
		   tnBufferSize = tnBufferSize + lnBytesRead
		   lnBufferReads = lnBufferReads + 1
		   THIS.OnFTPBufferUpdate(tnBufferSize,lnBufferReads,@lcReadBuffer)
		ENDIF
        IF (lnRetVal = 1 AND lnBytesRead = 0)
           *** Done
           EXIT
        ENDIF
        IF lnRetVal = 0
		   THIS.nError = GetLastError()
		   THIS.cErrorMsg = THIS.GetSystemErrorMsg()

           =FCLOSE(lhFileHandle)
           ERASE (lcTargetFile)
           EXIT
        ENDIF
        
        IF THIS.lCancelDownload
           =FCLOSE(lhFileHandle)
           ERASE (lcTargetFile)
           EXIT
        ENDIF
ENDDO

InternetCloseHandle(hFTPFile)

lnBufferSize = tnBufferSize
 
THIS.OnFTPBufferUpdate(0,-1,"")

=FCLOSE(lhFileHandle)

IF lnBufferSize # lnFileSize AND NOT THIS.lCancelDownload
	***Delete partial downloaded file
	ERASE (lcTargetFile)
	
	THIS.nError = -2
	THIS.cErrorMsg = "Download Partially"
	RETURN -2
ENDIF

RETURN 0
wwFTP.FTPSendFIleEx()
LPARAMETER lcSourceFile, lcTargetFile
LOCAL lhFile, lnRetVal, lnBytesRead, lnBufferReads, lcWriteBuffer, hFTPFile, lnFileSize, ;
	lnWebFileSize
LOCAL ARRAY laFiles[1]

DECLARE INTEGER FtpOpenFile ;
   IN WININET.DLL ;
   INTEGER hIPSession,;
   STRING @lpszFileName,;
   INTEGER dwAcessFlags,;
   INTEGER dwServiceFlags,;
   INTEGER dwContext

DECLARE INTEGER InternetWriteFile ;
   IN WININET.DLL ;
   INTEGER hFTPHandle,;
   STRING lcBuffer,;
   INTEGER cbBuffer,;
   INTEGER @cbBuffer

hFTPFile = FtpOpenFile(THIS.hFTPSession,lcTargetFile,;
                       GENERIC_WRITE,;
                       INTERNET_FLAG_RELOAD + FTP_TRANSFER_TYPE_BINARY,0)
IF hFTPFile = 0
   THIS.nError = GetLastError()
   THIS.cErrorMsg = THIS.GetSystemErrorMsg()
   RETURN THIS.nError
ENDIF

*** Read the file from disk
lhFile = FOPEN(lcSourceFile)
IF lhFile = -1
   THIS.cErrorMsg = "Source file doesn't exist or is in use..."
   THIS.nError = 1
   RETURN 1
ENDIF   

tnBufferSize = 0
lnBufferReads = 0
DO WHILE .T.
   *** Read one chunk at a time
   lcWriteBuffer = FRead(lhFile,THIS.nFTPWorkBufferSize)
   IF LEN(lcWriteBuffer) = 0
      EXIT
   ENDIF

   *** And write out each chunk
   lnSize=LEN(lcWriteBuffer)
   lnBytesRead = 0
   lnRetval=InternetWriteFile(hFTPFile,;
		   lcWriteBuffer,;
		   lnSize,;
		   @lnBytesRead)	

    IF lnRetVal = 1 AND lnBytesRead > 0
	   *** Update the input parameters - result buffer and size of buffer
	   tnBufferSize = tnBufferSize + lnBytesRead
	   lnBufferReads = lnBufferReads + 1
	   THIS.OnFTPBufferUpdate(tnBufferSize,lnBufferReads,"")
	ENDIF
	IF THIS.lCancelDownload
      THIS.cErrorMsg = "Download canceled by user"
	  EXIT
	ENDIF
    IF (lnRetVal = 1 AND lnBytesRead = 0)
      *** Done
      EXIT
    ENDIF
ENDDO

InternetCloseHandle(hFTPFile)	

lnBufferSize = tnBufferSize
THIS.OnFTPBufferUpdate(0,-1,"")

FCLOSE(lhFile)

IF NOT THIS.lCancelDownload

	IF ADIR(laFiles, lcSourceFile, "A") = 0
		THIS.nError = -3
		THIS.cErrorMsg = "Source file is missing"
		
		RETURN -3
	ENDIF

	lnFileSize = laFiles[1, 2]

	IF THIS.aFTPDir(@laFiles, lcTargetFile) = 0
		RETURN THIS.nError
	ENDIF

	lnWebFileSize = laFiles[1, 2]

	IF lnFileSize # lnWebFileSize
		***Delete file from web if upload partial
		THIS.FtpDeletefile(lcTargetFile)
		THIS.nError = -2
		THIS.cErrorMsg = "Upload Partially"
		
		RETURN -2
	ENDIF
ELSE
	THIS.FtpDeletefile(lcTargetFile)
ENDIF

RETURN 0
HTH
I am not the most powerful man in this world.
I am not the worst man in this world either.
I just as same as all of you.
I still need to learn from my mistakes...
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform