Skip to content

Latest commit

 

History

History
108 lines (80 loc) · 3.15 KB

sample_069.md

File metadata and controls

108 lines (80 loc) · 3.15 KB

Home

How to retrieve the size of a remote file (FTP)

Before you begin:

See also:


Code:

#DEFINE INTERNET_INVALID_PORT_NUMBER   0
#DEFINE INTERNET_OPEN_TYPE_DIRECT      1
#DEFINE INTERNET_SERVICE_FTP           1
#DEFINE FTP_TRANSFER_TYPE_ASCII        1
#DEFINE FTP_TRANSFER_TYPE_BINARY       2

#DEFINE GENERIC_READ 0x80000000
#DEFINE GENERIC_WRITE 0x40000000

    PUBLIC hOpen, hFtpSession
    DO declare

	IF connect2ftp ("ftp.???.???", "???", "???")
		lcRemoteFile = "fpttest/win32api.txt"

		hFile = FtpOpenFile(hFtpSession, lcRemoteFile,;
				GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 0)

		IF hFile <> 0
			LOCAL lnSizeHigh
			lnSizeHigh = 0
			? "Remote file size:", FtpGetFileSize (hFile, @lnSizeHigh)
			= InternetCloseHandle (hFile)
		ENDIF

		= InternetCloseHandle (hFtpSession)
		= InternetCloseHandle (hOpen)
	ENDIF

PROCEDURE declare
	DECLARE INTEGER InternetOpen IN wininet;
		STRING sAgent, INTEGER lAccessType,;
		STRING sProxyName, STRING sProxyBypass, STRING lFlags
	
	DECLARE INTEGER InternetCloseHandle IN wininet INTEGER hInet

	DECLARE INTEGER InternetConnect IN wininet;
		INTEGER hInternetSession, STRING sServerName,;
		INTEGER nServerPort, STRING sUsername, STRING sPassword,;
		INTEGER lService, INTEGER lFlags, INTEGER lContext

	DECLARE INTEGER FtpOpenFile IN wininet;
		INTEGER hFtpSession, STRING sFileName,;
		INTEGER lAccess, INTEGER lFlags, INTEGER lContext

	DECLARE INTEGER FtpGetFileSize IN wininet;
		INTEGER hFile, INTEGER @lpdwFileSizeHigh
RETURN

FUNCTION  connect2ftp (strHost, strUser, strPwd)
	* open access to Inet functions
	hOpen = InternetOpen ("w32rmsize",;
			INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)

	IF hOpen = 0
		? "Unable to get access to WinInet.Dll"
		RETURN .F.
	ENDIF

	* connect to FTP
	hFtpSession = InternetConnect (hOpen, strHost,;
		INTERNET_INVALID_PORT_NUMBER,;
		strUser, strPwd, INTERNET_SERVICE_FTP, 0, 0)

	IF hFtpSession = 0
	* close access to Inet functions and exit
		= InternetCloseHandle (hOpen)
		? "FTP " + strHost + " is not available"
		RETURN .F.
	ELSE
		? "Connected to " + strHost + " as: [" + strUser + ", *****]"
	ENDIF
RETURN .T.  

Listed functions:

FtpGetFileSize
FtpOpenFile
InternetCloseHandle
InternetConnect
InternetOpen

Comment:

The code does not process the high-order part lpdwFileSizeHigh. Process this part for obtaining sizes of files larger than 4 GBytes.


2013-Jul-08: WebRequestMethods.Ftp.GetFileSize fails with error 550: SIZE not allowed in ASCII mode.

Apparently there is no way to make the FtpWebRequest to send the TYPE I command before the SIZE ?.