 |
VFP 愛用者社區 本討論區為 Visual Foxpro 愛用者經驗交流的地方, 請多多利用"搜尋"的功能, 先查看看有無前例可循, 如果還有不懂的再發問. 部份主題有附加檔案, 須先註冊成為社區居民才可以下載.
|
上一篇主題 :: 下一篇主題 |
發表人 |
內容 |
蔡文華
註冊時間: 2005-10-31 文章: 118
第 1 樓
|
發表於: 星期五 十一月 11, 2005 10:06 am 文章主題: 有關FTP網路下載檔案的問題? |
|
|
我有一台電腦擁有固定IP,而且開放一個FTP提供給散在外面的程式回來更新檔案
但是現在遇到一個問題,如果外面程式是在一台也擁有對外IP的電腦內執行時可以讀取FTP內的檔案資料
如果程式是在一台只有虛擬IP的電腦內執行就無法順利讀取FTP內的檔案資料了,請各位高手是否有方法
解決,謝謝各位仙腳
我要下載前會先CHECK一下FTP是否有通,程式如下:
在我用紅色標出的地方即為本次問題所在
**************************************************************
*函數說明:讀取指定FTP目錄下所有檔案資訊
*函數名稱:FtpAdir(strhost,valport,struser,strpwd,strloaddir,@fileadir)
*參數說明:strhost 字串 ftp位址
* valport 數值 連接埠數值
* struser 字串 登入帳號
* strpwd 字串 登入密碼
* strloaddir 字串 FTP來源路徑(預設為/)
* fileadir 陣列變數 將所讀取之檔案陣列傳回
*傳回值 :空字串 完成下載動作
* 非空字串 有錯誤發生
**************************************************************
function FtpAdir(strhost,valport,struser,strpwd,strloaddir,fileadir)
&& 宣告 wininet.dll
Declare Integer InternetOpen In wininet; && 開啟 internet 連結
String sAgent, Integer lAccessType, String sProxyName,;
STRING sProxyBypass, String lFlags
Declare Integer InternetCloseHandle In wininet Integer hInet && 關閉 internet 連結
Declare Integer InternetConnect In wininet; && 連結 ftp or http
Integer hInternetSession, String sServerName,;
INTEGER nServerPort, String sUsername,;
STRING sPassword, Integer lService,;
INTEGER lFlags, Integer lContext
Declare Integer FtpGetCurrentDirectory In wininet; && 取得 ftp 目錄資料
Integer hFtpSession, String @lpszDir, Integer @lpdwCurDir
Declare Integer FtpFindFirstFile In wininet; && 取得目錄中的第一個檔案名稱
Integer hFtpSession, String lpszSearchFile,;
STRING @lpFindFileData, Integer dwFlags, Integer dwContent
Declare Integer InternetFindNextFile In wininet; && 找下一個檔案名稱
Integer hFind, String @lpvFindData
Declare Integer InternetReadFile In wininet.Dll;
INTEGER hFile, String @lpBuffer,;
INTEGER dwNumberOfBytesToRead, Integer @lpdwNumberOfBytesRead
Declare Integer FileTimeToSystemTime In kernel32.Dll; && 取得檔案日期資訊
String @lpFileTime, String @lpSystemTime
Declare Integer InternetReadFile In wininet.Dll; && 讀取 ftp 上的檔案
Integer hFile, String @lpBuffer,;
INTEGER dwNumberOfBytesToRead, Integer @lpdwNumberOfBytesRead
#Define INTERNET_INVALID_PORT_NUMBER 0
#Define INTERNET_OPEN_TYPE_DIRECT 1
#Define INTERNET_OPEN_TYPE_PROXY 3
#Define INTERNET_DEFAULT_FTP_PORT 22
#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
#Define INTERNET_OPEN_TYPE_PRECONFIG 0
#Define FTP_TRANSFER_TYPE_ASCII 1
#Define FTP_TRANSFER_TYPE_BINARY 2
#Define INTERNET_SERVICE_FTP 1
#Define INTERNET_SERVICE_GOPHER 2
#Define INTERNET_SERVICE_HTTP 3
#Define FILE_ATTRIBUTE_NORMAL 128 && 0x00000080
#Define INTERNET_FLAG_NEED_FILE 16
***** 開始連線
sAgent = "vfp7" && 自訂一個名稱(隨便)
sProxyName = 0 && 不使用Proxy server -->vb 不是 chr(0) 是用 null
sProxyBypass = 0 && 不回復訊息
lFlags = 0 && 預設值
* 開啟連結 (建立 internet session)
hOpen = InternetOpen (sAgent, INTERNET_OPEN_TYPE_DIRECT,;
sProxyName, sProxyBypass, lFlags)
If hOpen = 0 && 如果傳回0代表不成功
Return('WinInet.Dll 作業失敗')
Endif
* 設定登入的使用者名稱
strHost=IIF(EMPTY(strHost) .or. VARTYPE(strHost)="U","127.0.0.1",strHost) && 要登入的ftp位址或名稱
valport=IIF(EMPTY(valport),21,valport) && ftp 的通訊埠
strUser=IIF(EMPTY(strUser),"anonymous",strUser) && 使用者名稱
strPwd=IIF(EMPTY(strPwd),"anonymous",strPwd) && 使用者密碼
************** 連線至 ftp
hFtpSession = InternetConnect (hOpen, strHost,;
valport,;
strUser, strPwd,;
INTERNET_SERVICE_FTP, 0, 0)
If hFtpSession = 0
= InternetCloseHandle(hOpen) &&關閉連結
Return('登入失敗')
Endif
lcDirectory = Space(250)
lnLen = Len(lcDirectory)
&&FTP下載目錄所在位置
if empty(strloaddir) &&如果使用者沒有指定ftp直接抓取路徑,則採用FTP現在目錄名稱
If FtpGetCurrentDirectory(hFtpSession,@lcDirectory,@lnLen) = 1 &&取得FTP 現在目錄名稱
** hFtpSession 取得的回傳參數 ,lcDirectory 傳回的目錄名稱,lnLen 目錄名稱的字元數 在參數前有 @ 的是回傳值,使用前必須先宣告
FtpDir=Substr(lcDirectory,1,lnLen) && 取得目錄名稱
Else
= InternetCloseHandle (hFtpSession) && 關閉目前連線
= InternetCloseHandle (hOpen)
Return('取得目錄失敗')
Endif
else &&指定FTP讀取路徑
FtpDir = strloaddir &&給予指定之FTP路徑
endif
********* 以下開始取目錄資料
lnFound = 0
lpFindFileData = Repli (Chr(0), 320) &&
Dimen arr [1, 4] && 存放檔案資訊用 1 檔名 2 大小 3 日期時間 4 是否目錄
If Right(Alltrim(FtpDir),1)<>"/" &&判斷目錄後有沒 /
FtpDir=Alltrim(FtpDir)+"/"
Endif
lcMask=FtpDir+'*.*'
hFind = FtpFindFirstFile(hFtpSession,lcMask,@lpFindFileData, INTERNET_FLAG_NEED_FILE, 0)
&&如果是虛擬IP執行的話這裡的hFind都會傳回0代表沒有檔案;可是很確定的是如果是對外IP執行的話即可以抓到檔案
If hFind = 0
&&&&沒資料
= InternetCloseHandle (hFtpSession) && 關閉目前連線
= InternetCloseHandle (hOpen)
Return('沒有檔案')
Else
Findfile=0
Findfile=defstruct(lpFindFileData, hFind) && 使用defstruct() 把陣列中的資料寫到回覆成 text 碼
Endif
***********開始下載資料到本地
dimen fileadir[findfile,4]
for iii=1 to findfile
fileadir[iii,1] = arr[iii,1]
fileadir[iii,2] = arr[iii,2]
fileadir[iii,3] = arr[iii,3]
fileadir[iii,4] = arr[iii,4]
next
Erase arr
= InternetCloseHandle (hFtpSession) && 關閉目前連線
= InternetCloseHandle (hOpen) &&關閉通道
return('')
*******************************************************************
請各位仙腳幫幫忙,還有沒有什麼方法可以處理呢
 |
|
回頂端 |
|
 |
ckp6250
註冊時間: 2004-07-30 文章: 1645
第 2 樓
|
發表於: 星期五 十一月 11, 2005 2:21 pm 文章主題: |
|
|
有夠複雜,下載FTP不能用比較簡單的方法嗎? |
|
回頂端 |
|
 |
蔡文華
註冊時間: 2005-10-31 文章: 118
第 3 樓
|
發表於: 星期五 十一月 11, 2005 3:11 pm 文章主題: |
|
|
我不會比較簡單的...可否指導一下 |
|
回頂端 |
|
 |
goto-dream
註冊時間: 2004-05-11 文章: 909
第 4 樓
|
|
回頂端 |
|
 |
fschern
註冊時間: 2003-10-12 文章: 34
第 5 樓
|
發表於: 星期日 十二月 27, 2009 7:08 pm 文章主題: |
|
|
若Client端為虛擬IP時,Client的FTP程式必須採行"PASSIVE"(被動式)傳輸模式.
(例如IE的"工具→網際網路選項→進階"中即有"使用被動式FTP"選項)
問題原因應該是你在進行FTP協定連線時沒有指定採"PASSIVE"(被動式)傳輸模式所造成的。 |
|
回頂端 |
|
 |
garfield Site Admin

註冊時間: 2003-01-30 文章: 2160
第 6 樓
|
發表於: 星期一 十二月 28, 2009 10:44 am 文章主題: |
|
|
參考 http://crm.ittoolbox.com/groups/technical-functional/clarify-l/code-to-ftp-attachment-to-unix-server-157770
&& 宣告 wininet.dll
Declare Integer InternetConnect In wininet; && 連結 ftp or http
Integer hInternetSession, String sServerName,;
INTEGER nServerPort, String sUsername,;
STRING sPassword, Integer lService,;
INTEGER lFlags, Integer lContext
If we pass the value x8000000 in the lFlags parameter, the connection will use passive FTP semantics.
lngINetConn = InternetConnect(lngINet, “ftp.microsoft.com”, 0, _
“anonymous”, “wally@wallyworld.com”, 1, 0x8000000, 0) _________________ 利用>>搜尋<<的功能會比問的還要快得到答案. |
|
回頂端 |
|
 |
ufochen
註冊時間: 2003-09-17 文章: 166
第 7 樓
|
發表於: 星期一 二月 01, 2010 8:23 pm 文章主題: |
|
|
我以前也曾經碰到過與樓主相同的問題 , 後來我改用vfpconnection.fll
上傳下載都沒有問題 ! 參考看看 ! google 一下就可找到 vfpconnection.fll
以下是範例 :
*!* The code below is not designed to run all at once
*!* Run the individual examples seperately
********************************************************************
*!* Upload Examples
********************************************************************
SET LIBRARY TO (LOCFILE("vfpconnection.fll","FLL"))
*!* FILE
?FILEGet("File:///C:\Source.txt", "C:\Destination.txt", "MyProgress()", "MyTrace()")
*!* FTP
?FTPGet("FTP://UserName:Password@somedomain.com/directory/Source.zip", "C:\Destination.zip", "MyProgress()", "MyTrace()")
*!* FTPS
?FTPSGet("FTPS://UserName:Password@somedomain.com:21/directory/Source.zip", "C:\Destination.zip", "MyProgress()", "MyTrace()")
*!* HTTP
?HTTPGet("http://www.somedomain.com/Source.htm", "C:\Destination.htm", "MyProgress()", "MyTrace()")
*!* HTTPS
?HTTPSGet("https://www.somedomain.com/Source.htm", "C:\Destination.htm", "MyProgress()", "MyTrace()")
SET LIBRARY TO
********************************************************************
*!* Download Examples
********************************************************************
SET LIBRARY TO (LOCFILE("vfpconnection.fll","FLL"))
*!* FILE
?FILEPut("C:\Source.txt", "File:///C:\Destination.txt", "MyProgress()", "MyTrace()")
*!* FTP
?FTPPut("C:\Source.zip", "FTP://UserName:Password@somedomain.com/directory/Destination.zip", "MyProgress()", "MyTrace()")
*!* FTPS
?FTPSPut("C:\Source.zip", "FTPS://UserName:Password@somedomain.com:21/directory/Destination.zip", "MyProgress()", "MyTrace()")
*!* HTTP
?HTTPPut("C:\Source.htm", "http://www.somedomain.com/Destination.htm", "MyProgress()", "MyTrace()")
*!* HTTPS
?HTTPSPut("C:\Source.htm", "https://www.somedomain.com/Destination.htm", "MyProgress()", "MyTrace()")
SET LIBRARY TO
********************************************************************
*!* To String Examples
********************************************************************
SET LIBRARY TO (LOCFILE("vfpconnection.fll","FLL"))
*!* FILE
?FILEToStr("C:\Source.txt")
*!* FTP
?FTPTOSTR("FTP://UserName:Password@somedomain.com:21/directory/Source.txt")
*!* FTPS
?FTPSTOSTR("FTPS://UserName:Password@somedomain.com:21/directory/Source.txt")
*!* HTTP
?HTTPToStr("http://www.somedomain.com/Source.txt")
*!* HTTPS
?HTTPSToStr("https://www.somedomain.com/Source.txt")
SET LIBRARY TO
********************************************************************
*!* HTTP Post Examples
********************************************************************
*!* Simple HTTP Post
SET LIBRARY TO (LOCFILE("vfpconnection.fll","FLL"))
LOCAL lcPost
m.lcPost = "fname=John&lname=Smith"
?HttpSimplePost("http://www.snee.com/xml/crud/posttest.cgi", m.lcPost, "", "MyTrace()")
SET LIBRARY TO
*!* MultiPart/Form-Data HTTP Post
SET LIBRARY TO (LOCFILE("vfpconnection.fll","FLL"))
LOCAL ARRAY aryPost(2,2)
aryPost(1,1) = "fname" && name
aryPost(1,2) = "test first" && value
aryPost(2,1) = "lname" && name
aryPost(2,2) = "test last" && value
?HttpPost("http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi", @aryPost, "", "MyTrace()")
SET LIBRARY TO
********************************************************************
*!* FTP Commands Example
********************************************************************
*!* Rename FTP Directory
SET LIBRARY TO (LOCFILE("vfpconnection.fll","FLL"))
LOCAL ARRAY aryFTPCommands(4)
aryFTPCommands(1) = "CWD /"
aryFTPCommands(2) = "PWD"
aryFTPCommands(3) = "RNFR MyDir"
aryFTPCommands(4) = "RNTO RenamedMyDir"
?FTPCommands("FTP://username:password@somedomain.com/", @aryFTPCommands, "MyTrace()")
*!* ?FTPSCommands("FTPS://username:password@somedomain.com/", @aryFTPCommands, "MyTrace()")
SET LIBRARY TO
********************************************************************
*!* URL Encode/Decode Example
********************************************************************
SET LIBRARY TO (LOCFILE("vfpconnection.fll","FLL"))
LOCAL lcString, lcEncoded, lcDecoded
m.lcString = "A!B@C#D$E%F^G&H*I(J)K_L-M=N+O[P]Q{R}S|T\U:V;W'X,Y.Z<0>1/2?3??????"
m.lcEncoded = UrlEncode(m.lcString)
m.lcDecoded = UrlDecode(m.lcEncoded)
?"Escaped: " + m.lcEncoded
?"Unescaped: " + m.lcDecoded
?"Original : " + m.lcString
SET LIBRARY TO
********************************************************************
*!* Setting Timeouts Example
********************************************************************
SET LIBRARY TO (LOCFILE("vfpconnection.fll","FLL"))
SetConnectTimeout(30) && Default is 10 seconds
SetResponseTimeout(30) && Default is 10 seconds
SET LIBRARY TO
********************************************************************
*!* Datetime String to the number of seconds from Epoch (12 Midnight on January 1, 1970)
********************************************************************
SET LIBRARY TO (LOCFILE("vfpconnection.fll","FLL"))
?DateStrToEpochSec("Sun, 06 Nov 1994 08:49:37 GMT")
?DateStrToEpochSec("Sunday, 06-Nov-94 08:49:37 GMT")
?DateStrToEpochSec("Sun Nov 6 08:49:37 1994")
?DateStrToEpochSec("06 Nov 1994 08:49:37 GMT")
?DateStrToEpochSec("06-Nov-94 08:49:37 GMT")
?DateStrToEpochSec("Nov 6 08:49:37 1994")
?DateStrToEpochSec("06 Nov 1994 08:49:37")
?DateStrToEpochSec("06-Nov-94 08:49:37")
?DateStrToEpochSec("1994 Nov 6 08:49:37")
?DateStrToEpochSec("GMT 08:49:37 06-Nov-94 Sunday")
?DateStrToEpochSec("94 6 Nov 08:49:37")
?DateStrToEpochSec("1994 Nov 6")
?DateStrToEpochSec("06-Nov-94")
?DateStrToEpochSec("Sun Nov 6 94")
?DateStrToEpochSec("1994.Nov.6")
?DateStrToEpochSec("Sun/Nov/6/94/GMT")
?DateStrToEpochSec("Sun, 06 Nov 1994 08:49:37 CET")
?DateStrToEpochSec("06 Nov 1994 08:49:37 EST")
?DateStrToEpochSec("Sun, 12 Sep 2004 15:05:58 -0700")
?DateStrToEpochSec("Sat, 11 Sep 2004 21:32:11 +0200")
?DateStrToEpochSec("20040912 15:05:58 -0700")
?DateStrToEpochSec("20040911 +0200")
SET LIBRARY TO
********************************************************************
*!* Determine the version of libcurl and OpenSSL that were used for VFPConnection.fll
********************************************************************
SET LIBRARY TO (LOCFILE("vfpconnection.fll","FLL"))
?CURLVERSION()
SET LIBRARY TO
***********************
FUNCTION MyProgress() && Callback from the FLL - can be used to track operation progress
***********************
*!* You can create your own function, procedure or method to handle this and name it whatever you want.
*!* The nConnectTotalBytes and nConnectBytesSoFar are private variables created on-the-fly by the FLL
?m.nConnectTotalBytes
?m.nConnectBytesSoFar
ENDFUNC
***********************
FUNCTION MyTrace() && Callback from the FLL - used to provide a detailed trace of the operation
***********************
*!* You can create your own function, procedure or method to handle this and name it whatever you want.
*!* The nTraceDataType and cTraceData are private variables created on-the-fly by the FLL
#DEFINE TYPE_TEXT 0
#DEFINE TYPE_HEADER_IN 1
#DEFINE TYPE_HEADER_OUT 2
#DEFINE TYPE_DATA_IN 3
#DEFINE TYPE_DATA_OUT 4
#DEFINE TYPE_SSL_DATA_IN 5
#DEFINE TYPE_SSL_DATA_OUT 6
#DEFINE TYPE_END 7
?ICASE(m.nTraceDataType = TYPE_TEXT, "STATUS:", ;
m.nTraceDataType = TYPE_HEADER_IN, "<RECV HEADER: ", ;
m.nTraceDataType = TYPE_HEADER_OUT, ">SEND HEADER: ", ;
m.nTraceDataType = TYPE_DATA_IN, "<RECV DATA: ", ;
m.nTraceDataType = TYPE_DATA_OUT, ">SEND DATA: ", ;
m.nTraceDataType = TYPE_SSL_DATA_IN, "<RECV SSL DATA: ", ;
m.nTraceDataType = TYPE_SSL_DATA_OUT, ">SEND SSL DATA: ", ;
m.nTraceDataType = TYPE_END, "END: ", "UNKNOWN: ")
??m.cTraceData
ENDFUNC |
|
回頂端 |
|
 |
215001
註冊時間: 2003-06-11 文章: 393
第 8 樓
|
發表於: 星期四 二月 04, 2010 11:33 am 文章主題: |
|
|
我是用 API 方式下載,大致上還ok
但是我有用到 FtpFindFirstFile() 去搜尋不特定的檔案目錄,第1次去查詢 "*.txt" 檔案目錄時,會很慢,不知道問題在那裡?
對方的FTP是採用 FileZilla
我下 FtpFindFirstFile("test.txt"....) && 可以很快就找到檔案
我下 FtpFindFirstFile("test_*.txt"....) && 第1次查詢時,約需等15~20秒,都快以為當掉了,
但是第2次再查詢時,只需等2秒,不知道問題在那裡?
有看了 vfpConnection.fll 的範例文件,好像沒有類似 FtpFindFirstFile() 的指令 |
|
回頂端 |
|
 |
ezpos
註冊時間: 2011-04-20 文章: 323
第 9 樓
|
發表於: 星期五 八月 21, 2015 4:45 pm 文章主題: |
|
|
請問 vfpconnection.fll
FTPGet("FTP://UserName:Password@somedomain.com/directory/Source.zip", "C:\Destination.zip", "MyProgress()", "MyTrace()")
如何按一個按鈕 就能中斷 執行 _________________ ezPos收銀機 簡單好用低成本 http://www.ezpos.info
全新美觀的POS收銀機.POS軟硬體耗材.
軟體客制化.網站規劃....能賺錢的都可以找我
http://www.twelife.com 台灣生活網 |
|
回頂端 |
|
 |
ckp6250
註冊時間: 2004-07-30 文章: 1645
第 10 樓
|
發表於: 星期五 八月 21, 2015 5:05 pm 文章主題: |
|
|
ezpos 寫到: |
如何按一個按鈕 就能中斷 執行 |
請參閱help中的 DoEvents 指令
應該可行。 |
|
回頂端 |
|
 |
|
|
您 無法 在這個版面發表文章 您 無法 在這個版面回覆文章 您 無法 在這個版面編輯文章 您 無法 在這個版面刪除文章 您 無法 在這個版面進行投票 您 無法 在這個版面附加檔案 您 無法 在這個版面下載檔案
|
|