| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5031 人关注过本帖, 3 人收藏
标题:《求助》VFP程序中直接访问FTP服务器
只看楼主 加入收藏
jlak2002
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-11-25
收藏(3)
 问题点数:0 回复次数:12 
《求助》VFP程序中直接访问FTP服务器
我想在程序中直接访问FTP服务器!不要登录界面就执行FTP.TXT文件中的操作
我还想在程序中设定一个时间定时系统
请高手可以指导一下吗?
ftp.txt清单
open 192.168.0.1 21
test
test
bin
prompt
get 测试.txt
bye

[此贴子已经被作者于2005-11-25 23:00:01编辑过]

搜索更多相关主题的帖子: 程序中 VFP 服务器 FTP 
2005-11-25 22:44
fown
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:58
帖 子:1229
专家分:171
注 册:2005-5-26
收藏
得分:0 
可以参考一下红雨老师的API方法

传送命令到 ftp 上执行

-=-=-=-=-=-=-=-=-=-=-=-=-=-

* Using FtpCommand
* 传送命令到 FTP 上执行
*-----------------------
Clea
#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

Public hOpen, hFtpSession
Do decl && declare external functions

If connect2ftp ("202.105.49.131", "down", "down")
	*DO test1
	*DO test2
	Do test3

	= InternetCloseHandle (hFtpSession)
	= InternetCloseHandle (hOpen)
Endif
Return        && main

Procedure  test1
	* 根目录清单
	= execCmd (hFtpSession, "LIST", 1)
	Return
Endproc

Procedure  test2
	* 改变工作目录并显示
	* 事实上,以下两个命令可以合并为: "LIST dmfiles"
	= execCmd (hFtpSession, "CWD dmfiles", 0)    && 0 is important
	= execCmd (hFtpSession, "LIST", 1)
	Return
Endproc

Procedure  test3
	* 取回(下载)文件
	* 保证该文件存在你的 FTP 上
	= execCmd (hFtpSession, "CWD 红雨", 0)
	= execCmd (hFtpSession, "LIST", 1)
	= execCmd (hFtpSession, "MKD temptest", 0)
	= execCmd (hFtpSession, "RETR 日历控件.zip", 1)
	Return
Endproc

Function  connect2ftp (strHost, strUser, strPwd)
	* open access to Inet functions
	hOpen = InternetOpen ("vfp", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)

	If hOpen = 0
		? "找不到库 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 + " 失败"
		Return .F.
	Else
		? "连接到 " + strHost + " 用户: [" + strUser + "]密码:[" + strPwd + "]"
	Endif
	Return .T.
Endfunc

Function  execCmd (hSession, lcCommand, fExpectResponse)
	* note that ASCII type is hard-coded
	hFile = 0
	lnResult = FtpCommand (hFtpSession, fExpectResponse,;
		FTP_TRANSFER_TYPE_ASCII, lcCommand, 0, @hFile)

	If lnResult = 0
		? "命令执行错误: " + LTRIM(STR(GetLastError()))
		Return
	Endif

	If hFile <> 0
		* if there is a return - display it on the screen
		Set MEMOWIDTH TO 100
		lnBufsize = 128        && reading buffer size
		?
		publ fileinfo
        fileinfo = chr(13)+chr(10)
		Do WHILE .T.
			lcBuffer = REPLI (Chr(0), lnBufsize)
			lnBytesRead = 0

			If InternetReadFile (hFile, @lcBuffer, lnBufsize, @lnBytesRead) = 1
				lcBuffer = LEFT(lcBuffer, lnBytesRead)
				?? lcBuffer
				fileinfo = fileinfo + lcBuffer
				If lnBytesRead < lnBufsize
					Exit
				Endif
			Else
				Exit
			Endif
		Enddo
		= InternetCloseHandle (hFile)
	Endif
	Return
Endfunc

Procedure  decl
	Declare INTEGER GetLastError IN kernel32

	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 FtpCommand IN wininet;
		INTEGER   hConnect,;
		INTEGER   fExpectResponse,;
		INTEGER   dwFlags,;
		STRING    lpszCommand,;
		STRING  @ dwContext,;
		INTEGER @ phFtpCommand

	Declare INTEGER InternetReadFile IN wininet;
		INTEGER   hFile,;
		STRING  @ sBuffer,;
		INTEGER   lNumBytesToRead,;
		INTEGER @ dwNumberOfBytesRead
	Return
Endproc

在指定 ftp 上创建目录

-=-=-=-=-=-=-=-=-=-=-=-=-=-

* Creating a directory on the FTP
* 在指定 FTP 上创建目录
*---------------------------------
Clea
#Define INTERNET_INVALID_PORT_NUMBER  0
#Define INTERNET_OPEN_TYPE_DIRECT     1
#Define INTERNET_SERVICE_FTP          1
#Define INTERNET_SERVICE_GOPHER       2
#Define INTERNET_SERVICE_HTTP         3

Do doDeclare

* initialize access to Inet functions
hOpen = InternetOpen ("vfp", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)

If hOpen = 0
	? "找不到库 WinInet.Dll 的入口"
	Return
Endif

* put FTP connection settings here
strHost = "202.105.49.131"
strUser = "down"
strPwd  = "down"

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

If hFtpSession <> 0
	mydir = "红雨" + SUBSTR(SYS(3),5)
	If FtpCreateDirectory (hFtpSession, mydir) = 1
		? "新的目录 " + mydir + " 创建成功"
	Else
		? "不能创建新目录:" + mydir
	Endif
Else
	? "指定的 FTP 连接失败!!!"
Endif

* close FTP connection
= InternetCloseHandle (hFtpSession)
= InternetCloseHandle (hOpen)

Procedure  doDeclare
	Declare INTEGER InternetOpen IN "wininet.dll" ;
		STRING  sAgent,;
		INTEGER lAccessType,;
		STRING  sProxyName,;
		STRING  sProxyBypass,;
		STRING  lFlags
	Declare INTEGER InternetCloseHandle IN "wininet.dll" INTEGER hInet
	Declare INTEGER InternetConnect IN "wininet.dll" ;
		INTEGER hInternetSession,;
		STRING  sServerName,;
		INTEGER nServerPort,;
		STRING  sUsername,;
		STRING  sPassword,;
		INTEGER lService,;
		INTEGER lFlags,;
		INTEGER lContext
	Declare INTEGER FtpCreateDirectory IN "wininet.dll" ;
		INTEGER hFtpSession,;
		STRING  lpszDirectory
Endproc

从 ftp 上删除指定文件

-=-=-=-=-=-=-=-=-=-=-=-=-=-

* Deleting a file stored on the FTP server  
* 从 ftp 上删除指定文件
*-------------------------------------------------

clea
#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

Public hOpen, hFtpSession
Do decl

If connect2ftp ("202.105.49.131", "down", "down")

	* no wildcards accepted; valid file name only
	? FtpDeleteFile (hFtpSession, "红雨/temptest.zip" )

	= InternetCloseHandle (hFtpSession)
	= InternetCloseHandle (hOpen)
Endif

Procedure  decl
	Declare INTEGER InternetOpen IN wininet.dll;
		STRING  sAgent,;
		INTEGER lAccessType,;
		STRING  sProxyName,;
		STRING  sProxyBypass,;
		STRING  lFlags

	Declare INTEGER InternetCloseHandle IN wininet.dll INTEGER hInet

	Declare INTEGER InternetConnect IN wininet.dll;
		INTEGER hInternetSession,;
		STRING  sServerName,;
		INTEGER nServerPort,;
		STRING  sUsername,;
		STRING  sPassword,;
		INTEGER lService,;
		INTEGER lFlags,;
		INTEGER lContext

	Declare INTEGER FtpDeleteFile IN wininet.dll;
		INTEGER hConnect,;
		STRING  lpszFileName
	Return
Endproc

Function  connect2ftp (strHost, strUser, strPwd)
	* open access to Inet functions
	hOpen = InternetOpen ("vfp", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)

	If hOpen = 0
		? "找不到库 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 + " 无效!!!"
		Return .F.
	Else
		? "连接到 " + strHost + " as: [" + strUser + ", *****]"
	Endif
	Return .T.
Endfunc


有人说VFP不行了,我想说,你连VFP十分之一的功能都不会用,你怎么知道VFP不行?本人拒绝回答学生的问题我回答问题一般情况下只提供思路不提供代码,请理解
2005-11-26 10:05
fown
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:58
帖 子:1229
专家分:171
注 册:2005-5-26
收藏
得分:0 

从 ftp 上获得文件清单

-=-=-=-=-=-=-=-=-=-=-=-=-=-

* Retrieving list of files on the FTP directory
* 从 ftp 上获得文件清单
*--------------------------------------------
Clea
#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 INTERNET_FLAG_NEED_FILE       16
#Define FILE_ATTRIBUTE_DIRECTORY      16

Public hOpen, hFtpSession, arr
Do decl

If connect2ftp ("202.105.49.131", "down", "down")
	lcMask = "*"
	lcRemotePath = "."
	Dimen arr [1, 3]
	lnfiles = remoteDir2array (hFtpSession, lcRemotePath, lcMask, @arr)
	crea curs ftpfilelist (文件名 C(60), 文件大小 N(12), 文件时间 T)
	myfilelist = []
	myfilelist = myfilelist + padr("文件名",60) + padr("文件大小",12) + padr("文件时间",24) + chr(13) + chr(10)
	For i = 1 to lnfiles
	    insert into ftpfilelist (文件名, 文件大小, 文件时间) value (arr(i,1), arr(i,2), arr(i,3))
		myfilelist = myfilelist ;
			+ padr(arr(i,1),60) ;
			+ padr(ByteSize( arr(i,2) ),12) ;
			+ padr(ttoc(arr(i,3)),24) ;
			+ chr(13) + chr(10)
	Endfor
	= InternetCloseHandle (hFtpSession)
	= InternetCloseHandle (hOpen)
Endif
_Cliptext = myfilelist
Modi file sys(2015) nowa
Keyboard '{CTRL+V}'
Retu

*-------------------------------
Procedure  decl
	Declare INTEGER InternetOpen IN wininet.dll;
		STRING  sAgent,;
		INTEGER lAccessType,;
		STRING  sProxyName,;
		STRING  sProxyBypass,;
		STRING  lFlags

	Declare INTEGER InternetCloseHandle IN wininet.dll INTEGER hInet

	Declare INTEGER InternetConnect IN wininet.dll;
		INTEGER hInternetSession,;
		STRING  sServerName,;
		INTEGER nServerPort,;
		STRING  sUsername,;
		STRING  sPassword,;
		INTEGER lService,;
		INTEGER lFlags,;
		INTEGER lContext

	Declare INTEGER FtpFindFirstFile IN wininet.dll;
		INTEGER  hFtpSession,;
		STRING   lpszSearchFile,;
		STRING @ lpFindFileData,;
		INTEGER  dwFlags,;
		INTEGER  dwContent

	Declare INTEGER InternetFindNextFile IN wininet.dll;
		INTEGER  hFind,;
		STRING @ lpvFindData

	Declare INTEGER FtpGetCurrentDirectory IN wininet.dll;
		INTEGER   hFtpSession,;
		STRING  @ lpszDirectory,;
		INTEGER @ lpdwCurrentDirectory

	Declare INTEGER FtpSetCurrentDirectory IN wininet.dll;
		INTEGER   hFtpSession,;
		STRING  @ lpszDirectory

	Declare INTEGER FtpOpenFile IN wininet.dll;
		INTEGER hFtpSession,;
		STRING  sFileName,;
		INTEGER lAccess,;
		INTEGER lFlags,;
		INTEGER lContext

	Declare INTEGER FileTimeToSystemTime IN kernel32.dll;
		STRING @ lpFileTime,;
		STRING @ lpSystemTime

	*!*		Declare INTEGER intShl IN win32vfp INTEGER nSource, INTEGER nShift  && = bitlshift()
	*!*		Declare INTEGER intShr IN win32vfp INTEGER nSource, INTEGER nShift  && = bitrshift()
	*!*		Declare INTEGER intAnd IN win32vfp INTEGER nInt0,   INTEGER nInt1   && = bitand()
	*!*		Declare INTEGER intXor IN win32vfp INTEGER nInt0,   INTEGER nInt1   && = bitxor()
	*!*		Declare INTEGER intOr  IN win32vfp INTEGER nInt0,   INTEGER nInt1   && = bitor()
	*!*		Declare INTEGER intNot IN win32vfp INTEGER nInt                     && = bitnot()
	*!*     Declare INTEGER buf2int IN win32vfp STRING @ lpBuffer               && = buf2int()
	Return
Endproc

Function buf2int
	Parameters oldpc
	If type('oldpc')='N'
		lnBig = int(oldpc/256)
		lnSmall = oldpc - lnBig * 256
		Retu alltrim(chr(lnSmall))+alltrim(chr(lnBig))+chr(0)+chr(0)
	Else
		lnresult = 0
		lnlast = len(oldpc)
		For lni = 1 to lnlast
			lnresult = lnresult + asc(substr(oldpc, lni, 1)) * (256 ^ (lni - 1))
		Endf
		lnmsb = (lnlast * 8) - 1
		If bittest(lnresult, lnmsb)
			lnmax = (2 ^ (lnmsb + 1))
			lnresult = lnresult - lnmax
		Endif
		Retu lnresult
	Endi
Endfunc

Function  connect2ftp (strHost, strUser, strPwd)
	hOpen = InternetOpen ("vfp", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)
	If hOpen = 0    && 打开 Inet 函数库
		= messagebox( "找不到库 WinInet.Dll 的入口", 16, "红雨信息")
		Return .F.
	Endif
	hFtpSession = InternetConnect (hOpen, strHost, ;
		INTERNET_INVALID_PORT_NUMBER, strUser, strPwd, ;
		INTERNET_SERVICE_FTP, 0, 0)  && 连接到 FTP
	If hFtpSession = 0	 && 关闭 Inet 函数库,退出
		= InternetCloseHandle (hOpen)
		= messagebox( "连接 FTP." + strHost + " 失败", 16, "红雨信息")
		Return .F.
	Else
		? "连接到 " + strHost + " 用户名: [" + strUser + ", *****]"
	Endif
	Return .T.
Endfunc

Function  setCurrentDir (hConnection, lcNewdir)
	Return FtpSetCurrentDirectory (hConnection, @lcNewdir) = 1
Endfunc

Function  getCurrentDir (hConnection)
	lcDirectory = SPACE(250)
	lnLen = Len(lcDirectory)
	If FtpGetCurrentDirectory (hConnection, @lcDirectory, @lnLen) = 1
		Return LEFT(lcDirectory, lnLen)
	Else
		Return ""
	Endif
Endfunc

Function  remoteDir2array (hConnection, lcRemotePath, lcMask, arr)
	* 将所有远程 ftp 上的文件描述赋值数组
	lcStoredPath = getCurrentDir (hConnection)
	If Not setCurrentDir (hConnection, lcRemotePath)
		Return -1    && 路径没发现
	Endif
	* 模拟 API 结构的对象
	Struct = CREATEOBJECT ("struct_WIN32_FIND_DATA")
	* 找到第一个文件
	lnFound = 0
	lpFindFileData = REPLI (Chr(0), 320)
	hFind = FtpFindFirstFile (hConnection, lcMask, @lpFindFileData, INTERNET_FLAG_NEED_FILE, 0)
	If hFind <> 0
		Do WHILE .T.
			Struct.setValue (lpFindFileData)
			If Not struct.isDirectory()
				lnFound = lnFound + 1
				Dimen arr [lnFound, 3]
				arr [lnFound, 1] = struct.fileName
				arr [lnFound, 2] = struct.fileSizeLo
				arr [lnFound, 3] = struct.lastWriteTime
			Endif
			* 找下一个文件
			If InternetFindNextFile (hFind, @lpFindFileData) <> 1
				Exit
			Endif
		Enddo
		= InternetCloseHandle (hFind)
	Endif
	Release struct
	= setCurrentDir (hConnection, lcStoredPath)
	Return  lnFound
Endfunc

Function ByteSize (dw)
	Declare STRING StrFormatByteSize IN Shlwapi;
		INTEGER   dw,;
		STRING  @ pszBuf,;
		INTEGER   cchBuf
	pszBuf = SPACE(50)
	Retu StrFormatByteSize (m.dw, @pszBuf, Len(pszBuf))
Endfunc

Define CLASS struct_WIN32_FIND_DATA As Custom
	* 模拟 WIN32_FIND_DATA 结构的类
	Value            = ""
	fileAttributes   = 0
	creationTimeLo   = 0
	creationTimeHi   = 0
	lastAccessTimeHi = 0
	lastAccessTimeLo = 0
	lastWriteTimeHi  = 0
	lastWriteTimeLo  = 0
	fileSizeLo       = 0
	fileName         = ""
	creationTime     = CTOT ("")
	lastAccessTime   = CTOT ("")
	lastWriteTime    = CTOT ("")

	Procedure  setValue (lcValue)
		* 转换缓冲内容到对象属性
		This.value            = lcValue
		This.fileAttributes   = THIS.buf2num (THIS.value,  0, 4)
		This.creationTimeLo   = THIS.buf2num (THIS.value,  4, 4)
		This.creationTimeHi   = THIS.buf2num (THIS.value,  8, 4)
		This.lastAccessTimeHi = THIS.buf2num (THIS.value, 12, 4)
		This.lastAccessTimeLo = THIS.buf2num (THIS.value, 16, 4)
		This.lastWriteTimeHi  = THIS.buf2num (THIS.value, 20, 4)
		This.lastWriteTimeLo  = THIS.buf2num (THIS.value, 24, 4)
		This.fileSizeLo       = THIS.buf2num (THIS.value, 32, 4)
		This.creationTime     = THIS.ftime2dtime (SUBSTR(THIS.value,  5, 8))
		This.lastAccessTime   = THIS.ftime2dtime (SUBSTR(THIS.value, 13, 8))
		This.lastWriteTime    = THIS.ftime2dtime (SUBSTR(THIS.value, 21, 8))

		This.fileName = ALLTRIM(SUBSTR(THIS.value, 45,250))
		If AT(Chr(0), THIS.fileName) <> 0
			This.fileName = SUBSTR (THIS.fileName, 1, AT(Chr(0), THIS.fileName)-1)
		Endif
	Endproc

	Function  isDirectory
		Return bitAnd (THIS.fileAttributes,FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY
	Endfunc

	Protected FUNCTION  buf2num (lcBuffer, lnOffset, lnBytes)
		* 从缓冲转换 bytes 到一个数值
		lnResult = 0
		For ii=1 TO lnBytes
			lnResult = lnResult +;
				bitlshift(Asc(SUBSTR (lcBuffer, lnOffset+ii, 1)), (ii-1)*8)
		Endfor
		Return  lnResult

	Protected FUNCTION  ftime2dtime (lcFileTime)
		lcSystemTime = REPLI (Chr(0), 16)
		= FileTimeToSystemTime (@lcFileTime, @lcSystemTime)
		wYear   = THIS.buf2num (lcSystemTime,  0, 2)
		wMonth  = THIS.buf2num (lcSystemTime,  2, 2)
		wDay    = THIS.buf2num (lcSystemTime,  6, 2)
		wHour   = THIS.buf2num (lcSystemTime,  8, 2)
		wMinute = THIS.buf2num (lcSystemTime, 10, 2)
		wSecond = THIS.buf2num (lcSystemTime, 12, 2)
		lcDate = STRTRAN (STR(wMonth,2) + "/" + STR(wDay,2) + "/" + STR(wYear,4), " ","0")
		lcTime = STRTRAN (STR(wHour,2) + ":" + STR(wMinute,2) + ":" + STR(wSecond,2), " ","0")
		lcStoredSet = SET ("DATE")
		Set DATE TO MDY
		ltResult = CTOT (lcDate + " " + lcTime)
		Set DATE TO &lcStoredSet
		Return  ltResult
Enddefine

从 ftp 上下载文件

-=-=-=-=-=-=-=-=-=-=-=-=-=-

* How to download a file from the FTP server using FtpGetFile
* 使用 FtpGetFile 从 FTP 上下载文件
*--------------------------------------------------------------
Clea
* lAccessType - some values
#Define INTERNET_INVALID_PORT_NUMBER  0
#Define INTERNET_OPEN_TYPE_DIRECT     1
#Define INTERNET_OPEN_TYPE_PROXY      3
#Define INTERNET_DEFAULT_FTP_PORT     21

* lFlags: only a few
#Define INTERNET_FLAG_ASYNC             268435456     && &H10000000
#Define INTERNET_FLAG_FROM_CACHE        16777216    && &H1000000
#Define INTERNET_FLAG_OFFLINE           16777216
#Define INTERNET_FLAG_CACHE_IF_NET_FAIL 65536        && &H10000

* registry access settings
#Define INTERNET_OPEN_TYPE_PRECONFIG  0
#Define FTP_TRANSFER_TYPE_ASCII       1
#Define FTP_TRANSFER_TYPE_BINARY      2

* type of service to access
#Define INTERNET_SERVICE_FTP     1
#Define INTERNET_SERVICE_GOPHER  2
#Define INTERNET_SERVICE_HTTP    3

* file attributes
#Define FILE_ATTRIBUTE_NORMAL    128    && 0x00000080

Do decl

* you are free to choose any name, say "John Dow"
* server-side variable $AGENT is the target
sAgent = "hongyu"

sProxyName = Chr(0)        && I have no proxy
sProxyBypass = Chr(0)    && so there is nothing to bypass
lFlags = 0                && no flags used

* initialize access to Inet functions
hOpen = InternetOpen (sAgent, INTERNET_OPEN_TYPE_DIRECT,;
	sProxyName, sProxyBypass, lFlags)

If hOpen = 0
	? "找不到库 WinInet.Dll 的入口"
	Return
Else
	? "Wininet 句柄: " + LTRIM(STR(hOpen))
Endif

* connection parameters - you better put your data
strHost = "202.105.49.131"
strUser = "down"
strPwd  = "down"

* connecting to the 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 + " 无效"
	Return
Else
	? "连接到 " + strHost + " as: [" + strUser + ", *****]"
	? "FTP 连接句柄: " + LTRIM(STR(hFtpSession))
Endif

* downloading a file from the FTP
* no check whether the target file exists
lpszRemoteFile = "红雨/日历控件.zip"           && if it still exists
lpszNewFile    = "C:\back\日历控件test.zip"    && check the destination folder
fFailIfExists  = 0        && do not stop if the target already exists
dwContext      = 0

lnResult = FtpGetFile (hFtpSession, lpszRemoteFile, lpszNewFile,;
	fFailIfExists, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_ASCII,;
	dwContext)

If lnResult # 1
	? "下载文件失败!"
Endif

* close handles
= InternetCloseHandle (hFtpSession)
= InternetCloseHandle (hOpen)
Return        && main

Procedure  decl
	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 FtpGetFile IN wininet;
		INTEGER hFtpSession, STRING  lpszRemoteFile,;
		STRING  lpszNewFile, INTEGER fFailIfExists,;
		INTEGER dwFlagsAndAttributes,;
		INTEGER dwFlags, INTEGER dwContext
Endproc


有人说VFP不行了,我想说,你连VFP十分之一的功能都不会用,你怎么知道VFP不行?本人拒绝回答学生的问题我回答问题一般情况下只提供思路不提供代码,请理解
2005-11-26 10:07
fown
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:58
帖 子:1229
专家分:171
注 册:2005-5-26
收藏
得分:0 

上传文件到 ftp 上

-=-=-=-=-=-=-=-=-=-=-=-=-=-

* 上传文件到 ftp 上
*------------------------------------------------

Clea
#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

Public hOpen, hFtpSession
Do decl

* select FTP connection providing you an appropriate access level
* in all cases it can not be any "anonymous" access
If connect2ftp ("202.105.49.131", "down", "down")

	* select the existing source and valid target file names
	* these names are as an example only
	lcSource  = "E:\Hongyu\角色代理.prg"
	lcTarget  = "红雨/temptest.txt"
	lcRenamed = "红雨/角色代理.prg"

	* default transfer type selected -
	* the file is transfered exactly as it is

	If FtpPutFile (hFtpSession, lcSource,;
			lcTarget, FTP_TRANSFER_TYPE_BINARY, 0) = 1

		* if file (lcRenamed) already exists, most probably it would be replaced
		* with the new one during the renaming
		* at least my FTP server acts like this
		? FtpRenameFile (hFtpSession, lcTarget, lcRenamed)

	Endif

	= InternetCloseHandle (hFtpSession)
	= InternetCloseHandle (hOpen)
Endif

Procedure  decl
	Declare INTEGER InternetOpen IN wininet.dll;
		STRING  sAgent,;
		INTEGER lAccessType,;
		STRING  sProxyName,;
		STRING  sProxyBypass,;
		STRING  lFlags

	Declare INTEGER InternetCloseHandle IN wininet.dll INTEGER hInet

	Declare INTEGER InternetConnect IN wininet.dll;
		INTEGER hInternetSession,;
		STRING  sServerName,;
		INTEGER nServerPort,;
		STRING  sUsername,;
		STRING  sPassword,;
		INTEGER lService,;
		INTEGER lFlags,;
		INTEGER lContext

	Declare INTEGER FtpPutFile IN wininet.dll;
		INTEGER hConnect,;
		STRING  lpszLocalFile,;
		STRING  lpszNewRemoteFile,;
		INTEGER dwFlags,;
		INTEGER dwContext

	Declare INTEGER FtpRenameFile IN wininet.dll;
		INTEGER hConnect,;
		STRING  lpszExisting,;
		STRING  lpszNew

	Return
Endproc

Function  connect2ftp (strHost, strUser, strPwd)
	* open access to Inet functions
	hOpen = InternetOpen ("vfp", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)

	If hOpen = 0
		? "找不到库 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 + " 无效"
		Return .F.
	Else
		? "连接到 " + strHost + " as: [" + strUser + ", *****]"
	Endif
	Return .T.
Endfunc

有人说VFP不行了,我想说,你连VFP十分之一的功能都不会用,你怎么知道VFP不行?本人拒绝回答学生的问题我回答问题一般情况下只提供思路不提供代码,请理解
2005-11-26 10:07
真诚随我走
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2005-11-30
收藏
得分:0 
收藏
2005-11-30 15:30
wjmwaq
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-8-11
收藏
得分:0 
收藏
2008-08-11 09:17
hu9jj
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:红土地
等 级:贵宾
威 望:400
帖 子:11780
专家分:43421
注 册:2006-5-13
收藏
得分:0 
收藏了,谢谢楼主!还要谢谢楼上“挖”出了这个宝藏!

活到老,学到老!http://www.(该域名已经被ISP盗卖了)E-mail:hu-jj@
2008-08-12 12:57
mspanwei
Rank: 1
等 级:新手上路
威 望:1
帖 子:49
专家分:0
注 册:2010-1-5
收藏
得分:0 
正是大家所需的超级源代码,真是感谢楼主,更要感谢FOWN的分享,FOWN太棒了!!!!!
2010-01-15 05:42
jyw
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-3-9
收藏
得分:0 
珍贵资料,收藏.谢谢
2011-05-04 16:30
ILoveVFD
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:218
专家分:147
注 册:2015-5-2
收藏
得分:0 
收藏了,谢谢楼主!
2015-05-08 13:54
快速回复:《求助》VFP程序中直接访问FTP服务器
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021362 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved