当前位置: 网学 > 编程文档 > VB > 正文

用vb6的ActiveX控件实现异步下载

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/16
下载{$ArticleTitle}原创论文样式

序:笔者(airon,softWorker)注意到,在vb6中,要实现文件下载,一般用和方法都是使用第三方控件,比如IE控件呀,winscok呀,但在本文中,不用添加任何控件,也不引用任何object,就可实现文件下载,而且程序不支持文件下载进度,捕获下载错误,激活下载完成事件等。

具体方法:
1.新建一VB6工程(默认有一个Form1窗体)
2.选择工程菜单的“添加用户控件”来添加一个用户控件。
3.更改Activex用户控件的名称,更改为 Downloader  (此项可省) 。
4.输入代码:(在用户控件的代码窗口中)

Option Explicit
Event DownloadProgress(CurBytes As Long, MaxBytes As Long, SaveFile As String)
Event DownloadError(SaveFile As String)
Event DownloadComplete(MaxBytes As Long, SaveFile As String)
''Public downStat As Boolean


Public Function CancelAsyncRead() As Boolean
    On Error Resume Next
    UserControl.CancelAsyncRead
End Function
''Private Sub Timer1_Timer()
''    If Not downStat Then
''        Timer1.Enabled = False
''        Exit Sub
''    End If
''    Static Cs As Integer
''    If Cs > 2 Then Cs = 0
''    UserControl.Picture = P1(Cs).Picture
''    Cs = Cs + 1
''    DoEvents
''End Sub
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
    On Error Resume Next
    Dim f() As Byte, fn As Long
    If AsyncProp.BytesMax <> 0 Then
        fn = FreeFile
        f = AsyncProp.Value
        Open AsyncProp.PropertyName For Binary Access Write As #fn
        Put #fn, , f
        Close #fn
    Else
        RaiseEvent DownloadError(AsyncProp.PropertyName)
    End If
    RaiseEvent DownloadComplete(CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
    downStat = False
End Sub
Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)
    On Error Resume Next
    If AsyncProp.BytesMax <> 0 Then
        RaiseEvent DownloadProgress(CLng(AsyncProp.BytesRead), CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
        downStat = True: Timer1.Enabled = True
    End If
End Sub
''Private Sub UserControl_Resize()
''    SizeIt
''End Sub
Public Sub BeginDownload(url As String, SaveFile As String)
    On Error GoTo ErrorBeginDownload
    downStat = True
    UserControl.AsyncRead url, vbAsyncTypeByteArray, SaveFile, vbAsyncReadForceUpdate
   

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号