我要在PB7中实现FTP的下载功能。
1。使用 mftpx.ocx(Mabry Software, Inc.)
下面是我的代码:
if ib_con =false then //判断是否已连接
ole_1.object.connect() //连接
if ib_con then //如果连接成功则继续,ib_con在connented事件中进行操作
ole_1.object.changedir(is_sourcedir) //在ftp上转到源文件夹
ole_1.object.GetFilenameList() //获得文件列表
ole_1.object.changedir( "/ ")
if is_file_list <> " " then //is_file_list包含所有文件名称
DO WHILE is_file_list <> " " //判断is_file_list是否为空
ls_temp = split(is_file_list, "; ") //获取单个文件名
ole_1.object.getfile(is_sourcedir+ls_temp, ls_temp) //从ftp上获得文件到本地,这里产生问题
ole_1.object.delete(is_sourcedir+ls_temp) //无问题
LOOP
end if
ole_1.object.disconnect() //中断连接
ib_con = false
end if
经过我的测试 ole_1.object.getfile(is_sourcedir+ls_temp, ls_temp) 的问题是最后一个变量ls_temp这里,也就是本地文件打不开。如果在这句之前加上 fileopen(ls_temp) ,经过调试这句的返回值为 -1,也就是PB自己的函数也无法操作,PB无法打开本地文件(按道理本应自动新建文件ls_temp),但是控件操作ftp无问题。但是如果我把变量ls_temp换成静态字符串 如 "1.txt ",则
ole_1.object.getfile(is_sourcedir+ls_temp, "1.txt ")可以成功。
但是如果代码没有去获取文件列表,一开始就制定一个文件则可以成功:
string ls_temp = "1.txt " //声明文件名
if ib_con =false then
ole_1.object.connect()
if ib_con then
ole_1.object.getfile(is_sourcedir+ls_temp, ls_temp) //无问题
ole_1.object.delete(is_sourcedir+ls_temp) //无问题
end if
ole_1.object.disconnect() //中断连接
ib_con = false
end if
这段代码则可以成功!!! (我觉得会不会是getfilenamelist得到的文件名字符串会不会fileopen不能解析??但是我调试时,文件名都能看得到。) 让我很不解,希望大虾们能指点我啊!!愁死了!! 谢谢了!!
2. msinet.ocx( Internet Transfer)
看以前文章说,这个控件的执行是异步的,所以我想知道在执行ole_1.object.Execute( " ", "GET scr\1.txt 1.txt ", " ", " ")时,如何判断下载完成???
我知道可以用 stillexecuting 属性:
ole_1.object.Execute( " ", "GET scr\1.txt 1.txt ", " ", " ")
Do While ole_1.object.stillexecuting
Loop
我执行时,这句话一直在循环,实际上我的文件早就下载完了(文件只有8byte),我想这个属性对大文件或许有用,但是对于小文件或许不能明确的表示出来。我也想用过statechanged中的state来作标记,但是发现state=12(从网上看到代表:该请求已经完成,并且所有数据均已接收到。)也不能唯一的指定文件下载完了。因为我测试时,下载完的代码有8,11,12这三种情况。但是却不能用他们,因为他们有各自的含义,但不适用于下载完成。
因为我要连续下载多个文件,但是前一个文件不下载完,无法下载下个文件,所以我又困惑阿!! 谢谢大虾了!!
3。有什么好用的ftp 控件(适用于pb7)??推荐个,谢谢!!
------解决方案--------------------
Do While ole_1.object.stillexecuting
Loop
改成
Do While ole_1.object.stillexecuting
yield()
Loop