Next Topic

Previous Topic

Book Contents

Sample Script: TLuaFTPClient

--Script connects to a FTP server and download content of file
ftp = TLuaFTPClient();

--Enter the username and password for the session
sUsername = "myusername" sPassword = "mypassword"

--Connect to FTP server using username and password
iRet = ftp:Connect(sUsername,sPassword,21)

--Check return value from server
if iRet == 0 then
	--Failed to connect, print why
	iRet = GetLastError()
	sErrorString = FormatErrorString(iRet)
	sErrString = "Error when connecting to FTP server, error: "..sErrorString 	SetExitStatus(sErrString,false)

else
	--Open a file on the FTP server that we know exist
	sFilename = "update.vcf"

	--Open file, do not create it, use text mode
	iRet = ftp:OpenFile(sFilename,false,true)
	if iRet == 0 then
		sErrString = "Cannot open file "..sFilename SetExitStatus(sErrString,false)
	else
		iMaxSize = 1024*16
		--Read a number of bytes from the file
		--Note here that we are using the special lua return value convention
		--Read returns one string and the size of the string
		sFilecontent, iMaxSize = ftp:Read(iMaxSize)
		print("Size of content: "..iMaxSize.."\n")
		print(sFilecontent)
		--Close file so we can open a new file later or close the session
		ftp:CloseFile()
		SetExitStatus("Test ok",true)
	end
end

--Close FTP session
ftp:Close()