标题:从FTP服务器上下载文件失败,请帮忙分析?
取消只看楼主
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
结帖率:96.03%
已结贴  问题点数:20 回复次数:12 
从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

    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

* 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 = "hz230674.ftp.
strUser = "hz230674"
strPwd  = "*****"

* 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 = "/htdocs/images/mylove2.txt"           && if it still exists
lpszNewFile    = "D:\myfolder"    && 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

[此贴子已经被作者于2017-8-31 20:37编辑过]

搜索更多相关主题的帖子: FTP the Define INTEGER STRING 
2017-08-31 16:08
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
得分:0 
回复 2楼 吹水佬

****** 代码重新整理如下:*******

* 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

    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

* 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 = "hz230674.ftp.
strUser = "hz230674"
strPwd  = "******"

* 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 = "/htdocs/images/mylove2.txt"           && if it still exists
lpszNewFile    = "D:\myfolder"    && 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





[此贴子已经被作者于2017-8-31 20:34编辑过]

2017-08-31 20:32
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
得分:0 
回复 2楼 吹水佬
服务器密码已发送给您,拜托您测试一下。
2017-08-31 20:41
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
得分:0 
我的电脑 D:\myfolder 中并没有mylove2文件。
2017-08-31 21:29
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
得分:0 
回复 6楼 吹水佬
D:\myfolder 中没有mylove2 文件。

2017-08-31 21:34
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
得分:0 
回复 10楼 吹水佬

我这边显示 下载失败!
十分感谢你!祝您晚安
2017-08-31 22:20
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
得分:0 
回复 5楼 吹水佬
关闭防火墙后试了一下,结果仍是失败。



代码是放在表单初始化中运行的。
路径有问题吗?快崩溃了!


[此贴子已经被作者于2017-9-1 14:35编辑过]

2017-09-01 14:31
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
得分:0 
回复 13楼 吹水佬
1新建一个程序mytry,程序代码三楼代码。
2在命令中输入
set proc to mytry
do mytry
set proc to
是这样吗?
2017-09-01 20:24
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
得分:0 
回复 17楼 吹水佬

我这边也是win10 + vfp9 在vfp环境下测试如下:



仍就是下载失败!
2017-09-01 21:22
su0527
Rank: 2
等 级:论坛游民
帖 子:452
专家分:79
注 册:2015-1-10
得分:0 
回复 19楼 吹水佬
照您指点,操作后显示如下:



错误号:5
查找错误代码,如下:



什么意思呢?帮忙分析
2017-09-02 06:34



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-480384-1-1.html




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

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