`

VB 用API下载文件实例

    博客分类:
  • vb
阅读更多
'#############################
'**
'** 文件 frmDownLoad.frm 的内容
'**
'#############################
VERSION 5.00
Begin VB.Form frmDownLoad
BorderStyle =
1 'Fixed Single
Caption = "Form1"
ClientHeight = 2880
ClientLeft = 45
ClientTop = 330
ClientWidth = 6375
BeginProperty Font
Name =
"宋体"
Size = 9
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
LinkTopic =
"文件下载"
MaxButton = 0 'False
ScaleHeight = 2880
ScaleWidth = 6375
StartUpPosition = 2 'CenterScreen
Begin VB.CommandButton cmdStop
Caption =
"停止"
Enabled = 0 'False
Height = 480
Left = 1860
TabIndex = 6
Top = 2160
Width = 1365
End
Begin VB.CommandButton cmdStart
Caption =
"开始"
Height = 480
Left = 165
TabIndex = 5
Top = 2160
Width = 1365
End
Begin VB.TextBox txtFile
Height =
330
Left = 750
TabIndex = 3
Top = 705
Width = 5445
End
Begin VB.TextBox txtURL
Height =
330
Left = 750
TabIndex = 1
Top = 285
Width = 5445
End
Begin VB.Label lblCount
BackStyle =
0 'Transparent
Caption = "下载"
Height = 180
Left = 180
TabIndex = 4
Top = 1245
Width = 5130
End
Begin VB.Label Label1
AutoSize = -
1 'True
Caption = "文件:"
Height = 180
Left = 195
TabIndex = 2
Top = 780
Width = 450
End
Begin VB.Label lblURL
AutoSize = -
1 'True
Caption = "URL:"
Height = 180
Left = 195
TabIndex = 0
Top = 360
Width = 360
End
End
Attribute VB_Name = "frmDownLoad"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option
Explicit

Private Declare Function StrFormatByteSize Lib "shlwapi" Alias _
"StrFormatByteSizeA" (ByVal dw As Long, ByVal pszBuf As String, ByRef _
cchBuf
As Long) As String

Private Declare Function
InternetOpen Lib "wininet.dll" _
Alias "InternetOpenA" (ByVal sAgent As String, _
ByVal lAccessType As Long, ByVal sProxyName As String, _
ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function
InternetOpenUrl Lib "wininet.dll" _
Alias "InternetOpenUrlA" (ByVal hOpen As Long, _
ByVal surl As String, ByVal sHeaders As String, _
ByVal lLength As Long, ByVal lFlags As Long, _
ByVal lContext As Long) As Long

Private Declare Function
HttpOpenRequest Lib "wininet.dll" _
Alias "HttpOpenRequestA" _
(
ByVal hInternetSession As Long, _
ByVal lpszVerb As String, _
ByVal lpszObjectName As String, _
ByVal lpszVersion As String, _
ByVal lpszReferer As String, _
ByVal lpszAcceptTypes As Long, _
ByVal dwFlags As Long, _
ByVal dwContext As Long) As Long
Private Declare Function
InternetConnect Lib "wininet.dll" _
Alias "InternetConnectA" _
(
ByVal hInternetSession As Long, _
ByVal lpszServerName As String, _
ByVal nProxyPort As Integer, _
ByVal lpszUsername As String, _
ByVal lpszPassword As String, _
ByVal dwService As Long, _
ByVal dwFlags As Long, _
ByVal dwContext As Long) As Long
Private Declare Function
HttpSendRequest Lib "wininet.dll" _
Alias "HttpSendRequestA" _
(
ByVal hHttpRequest As Long, _
ByVal sHeaders As String, _
ByVal lHeadersLength As Long, _
ByVal sOptional As String, _
ByVal lOptionalLength As Long) As Boolean

Private Declare Function
InternetReadFile Lib "wininet.dll" _
(
ByVal hFile As Long, ByRef sBuffer As Byte, _
ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) _
As Integer
Private Declare Function
InternetCloseHandle Lib "wininet.dll" _
(
ByVal hInet As Long) As Integer

Private Declare Function
GetLastError Lib "kernel32" () As Long

' Adds one or more HTTP request headers to the HTTP request handle.
'Public Declare Function HttpAddRequestHeaders Lib "wininet.dll" Alias "HttpAddRequestHeadersA" _
'(ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, _
'ByVal lModifiers As Long) As Integer
Private bolStop As Boolean
' 然后,我们可以得到包含了一份详细说明的URL文本文件,它显示在下面的函数中:
Public Function DownloadFile(ByVal surl As String, ByVal strFile As String) As Long
Dim
s As String
Dim
hOpen As Long
Dim
hOpenUrl As Long
Dim
bDoLoop As Boolean
Dim
bRet As Boolean
Dim
intFH As Integer

Dim
sReadBuffer() As Byte
Dim
lNumberOfBytesRead As Long
Dim
lCount As Long
Dim
myCount As New clsCount
Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Const INTERNET_OPEN_TYPE_DIRECT = 1
Const INTERNET_OPEN_TYPE_PROXY = 3
Const scUserAgent = "VB OpenUrl"
Const INTERNET_FLAG_RELOAD = &H80000000

lblCount.Caption = "正在连接服务器..."
lblCount.Refresh
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString,
0)
hOpenUrl = InternetOpenUrl(hOpen, surl, vbNullString,
0, INTERNET_FLAG_RELOAD, 0)
lCount =
0

If hOpen <> 0 And hOpenUrl <> 0 Then
intFH = FreeFile
If Dir(strFile) <> "" Then
VBA.FileSystem.Kill strFile
End If
Open strFile For Binary As #intFH
myCount.Clear
Do While True
ReDim
sReadBuffer(2048)
bRet = InternetReadFile(hOpenUrl, sReadBuffer(
0), 2048, lNumberOfBytesRead)
If lNumberOfBytesRead > 0 And bRet = True Then
'if lnumberofbytesread<>2048 then
ReDim Preserve sReadBuffer(0 To lNumberOfBytesRead - 1)
Put
#intFH, , sReadBuffer
'
' buf.AddRange sReadBuffer, 0, lNumberOfBytesRead - 1
lCount = lCount + lNumberOfBytesRead
myCount.Count lNumberOfBytesRead
lblCount.Caption =
"已下载 " & VBStrFormatByteSize(lCount) & " [ " & VBStrFormatByteSize(myCount.Speed) & " /秒 ]"
lblCount.Refresh
Else
Exit Do
End If
bolStop = False
DoEvents
If bolStop = True Then
Exit Do
End If
Loop
Close #intFH
lblCount.Caption = "共下载 " & lCount & " 字节"
Else
lblCount.Caption = "打开URL错误"
End If

If
hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)
If hOpen <> 0 Then InternetCloseHandle (hOpen)
Set myCount = Nothing
DownloadFile = lCount
End Function
Private Sub
cmdStart_Click()
txtURL.Enabled =
False
txtFile.Enabled = False
cmdStart.Enabled = False
cmdStop.Enabled = True
DownloadFile txtURL.Text, txtFile.Text
cmdStop.Enabled =
False
cmdStart.Enabled = True
txtFile.Enabled = True
txtURL.Enabled = True

End Sub
Private Sub
cmdStop_Click()
bolStop =
True
End Sub
Private Sub
SetText(ByVal txt As TextBox)
txt.Text = GetSetting(App.Title, Me.Name, txt.Name)
End Sub
Private Sub
SaveText(ByVal txt As TextBox)
SaveSetting App.Title, Me.Name, txt.Name, txt.Text
End Sub
Private Sub
Form_Load()
SetText Me.txtFile
SetText Me.txtURL
End Sub
Private Sub
Form_Unload(Cancel As Integer)
SaveText Me.txtFile
SaveText Me.txtURL
End Sub

Private Function
VBStrFormatByteSize(ByVal lngSize As Long) As String
Dim
strSize As String * 128
Dim strData As String
Dim
lPos As Long
StrFormatByteSize lngSize, strSize, 128
lPos = InStr(1, strSize, Chr$(0))
strData = Left$(strSize, lPos -
1)
If lngSize > 1024 Then
strData = lngSize & "字节(" & strData & ")"
End If
VBStrFormatByteSize = strData
End Function

 

'########################
'**
'** 文件 clsCount.cls 的内容
'**
'########################
VERSION 1.0 CLASS
BEGIN
MultiUse = -
1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsCount"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option
Explicit
'******************************************************************************
'**
'** 用于计算速度的类模块
'**
'** 该类模块设定一个计数器,由程序不断的累计数据,并根据所花时间计算数据
'**
'** 编制: 袁永福
'** 时间: 2002-4-2
'**
'******************************************************************************
Private Declare Function GetTickCount Lib "kernel32" () As Long

Private
lngCountStart As Long
Private
lngCountCurrent As Long
Private
lngCountLast As Long
Private
lngSpeed As Long
Private
lngTickStart As Long
Private
lngTickCurrent As Long
Private
lngTickLast As Long
'Public StopCount As Boolean
'** 获得计数数据 **************************************************************
'** 累计初始值
Public Property Get CountStart() As Long
CountStart = lngCountStart
End Property
'** 累计终止值
Public Property Get CountEnd() As Long
CountEnd = lngCountCurrent
End Property
'** 累计总的速度
Public Property Get TotalSpeed() As Long
If
lngTickCurrent = lngTickStart Then
TotalSpeed = 0
Else
TotalSpeed = (lngCountCurrent - lngCountStart) / ((lngTickCurrent - lngTickStart) / 1000)
End If
End Property
'** 累计所花毫秒数
Public Property Get TotalTickCount() As Long
TotalTickCount = lngTickCurrent - lngTickStart
End Property
'** 清除所有数据 **************************************************************
Public Sub Clear()
lngCountStart =
0
lngCountCurrent = 0
lngCountLast = 0

lngSpeed = 0

lngTickStart = GetTickCount()
lngTickCurrent = lngTickStart
lngTickLast = lngTickStart

'StopCount = False
End Sub
'** 设置累计基数
Public Property Let CountStart(ByVal lStart As Long)
lngCountStart = lStart
lngCountCurrent = lStart
End Property
'** 累加数据 **
Public Sub Count(Optional ByVal lCount As Long = 1)
lngCountCurrent = lngCountCurrent + lCount
lngTickCurrent = GetTickCount()
End Sub

'** 获得速度 **
Public Property Get Speed() As Long
'lngTickCurrent = GetTickCount()
If lngTickLast = lngTickCurrent Then
Speed = lngSpeed
Else
Speed = (lngCountCurrent - lngCountLast) / ((lngTickCurrent - lngTickLast) / 1000)
lngSpeed = Speed
lngTickLast = lngTickCurrent
lngCountLast = lngCountCurrent
End If
End Property

'** 数据是否是最新更新的 **
Public Property Get NewSpeed() As Boolean
Dim
bolNew As Boolean
If
lngTickCurrent > lngTickLast + 1000 Then
bolNew = True
Else
bolNew = False
End If
NewSpeed = bolNew
End Property

 

分享到:
评论

相关推荐

    vb调用URLDownloadToFile Api下载文件实例

    `URLDownloadToFile` API函数是Windows系统提供的一个强大工具,它允许VB应用程序直接从指定的URL下载文件到本地磁盘。这个API函数是Internet Explorer组件的一部分,包含在Wininet.dll库中。 `URLDownloadToFile`...

    VB使用URLDownloadToFile下载文件实例

    在VB(Visual Basic)编程中,`URLDownloadToFile`是一个非常实用的API函数,它允许开发者从指定的URL下载文件到本地系统。这个函数属于Windows操作系统内置的`Wininet.dll`库,它提供了网络访问的功能,包括HTTP、...

    VB 纯API文件打开与保存对话框_vb另存为api_VB纯API文件打开与保存对话框_源码

    在"VB 纯API文件打开与保存对话框"的示例中,核心API函数可能包括`GetOpenFileName`和`GetSaveFileName`,这两个函数分别用于打开文件对话框和保存文件对话框。它们允许用户从文件系统中选择一个文件,或者指定一个...

    vb API帮助文件

    `vbapi.chm`可能是一个包含VB API相关函数和方法的离线帮助文档,`.chm`是Microsoft的帮助文件格式,用于存储HTML和相关资源,便于用户查找和学习API的使用方法。 `VisualBasic手册.chm`很可能是Visual Basic的官方...

    VB6.0官方API文本查看器

    API的使用在VB6.0编程中至关重要,例如,如果需要进行文件操作,如创建、读取或删除文件,可以通过调用CreateFile、ReadFile和DeleteFile等API函数实现。又如,如果需要实现系统级别的交互,如控制鼠标和键盘,可以...

    vb API 函数使用示例大全

    1. **Example102-复制文件(CopyFile)**: 这个示例展示了如何使用API函数`CopyFile`来复制一个文件到另一个位置。在VB中,通常使用`FileCopy`函数,但`CopyFile`提供了更多的选项,如覆盖已存在文件的能力。 2. **...

    VB6.0 API大全实例应有尽有

    API实例则展示了如何在VB6.0程序中有效地使用这些API函数,帮助开发者理解和应用API来解决实际问题。 API实例.doc可能包含的内容有: 1. **文件操作**:如使用CreateFile、ReadFile、WriteFile等函数进行文件读写...

    VBapi100个经典实例(功能齐全)

    VBapi100个经典实例(功能齐全)这个资源涵盖了Visual Basic (VB)编程中使用API函数的各种实用示例。API(Application Programming Interface)是操作系统为开发者提供的接口,允许程序与操作系统进行交互,实现更...

    VB(Visual Basic)API Viewer 单独文件

    通过这个工具,用户可以查找特定的API函数,查看其参数、返回值和使用示例,从而在VB代码中正确地调用这些函数。 而WIN32API.TXT 文件可能是一个文本文件,包含了Win32 API的详细信息,包括函数声明、参数说明以及...

    一百多个vb api详细实例 让你快速掌握常用api

    "一百多个vb api详细实例 让你快速掌握常用api" 这个资源提供了丰富的VB API应用示例,帮助初学者和有经验的开发者更深入地理解如何利用API来增强VB程序的功能。 API在VB中的应用是多样的,涵盖了各种系统操作和...

    VB利用官方api读写JSON数据格式文件简单实例

    在VB(Visual Basic)编程环境中,使用官方API来读写JSON数据格式文件是常见的操作,尤其是在处理网络数据交换或者存储配置信息时。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和...

    vb使用API控制ListBox,突破32767行数的限制[vb6.0可用]

    在VB6.0中,ListBox控件默认支持的最大行数为32767行,这对于某些需要显示大量数据的应用程序...文件“API控制ListBox[VB6.0可用].rar”应该包含了相关的代码示例和详细说明,可以帮助开发者更好地理解和应用这个技术。

    VB调用API函数方法详解

    这通常需要查阅API函数的文档,如VBAPI函数参考手册。 为了更好地掌握这项技能,建议初学者可以阅读专门介绍VB调用API的书籍,这些书籍通常会详细介绍如何声明、调用API函数,以及如何处理各种情况。此外,实践是...

    VB.NET API浏览器

    VB.NET API浏览器是一款专为VB.NET开发者设计的实用工具,旨在简化API函数的查找和使用过程。这款浏览器能够自动生成适用于VB.NET环境的API函数,极大地提升了开发效率和代码的可读性。API(Application Programming...

    VB API 实现FTP上传下载源代码

    在VB(Visual Basic)环境中,我们可以使用API(Application Programming Interface)来实现FTP的功能,如文件的上传和下载。API是操作系统提供的一组函数,允许开发者直接与操作系统交互,执行特定任务。在VB中,...

    一个演示VB API高速搜索文件的程序.rar

    这个程序可能是从一本VB教程或实例集合中提取的,目的是为了让VB初学者了解如何使用API进行文件操作,尤其是文件搜索。代码中包含了详细的注释,以帮助学习者理解每一部分的功能和用法。 标签“VB源码-文件操作”...

    已经实现了:(含源码)VB利用官方api读写JSON数据格式文件简单实例.zip

    本实例将详细讲解如何利用官方API实现VB对JSON的读写操作,帮助开发者更好地理解这一过程。 首先,JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它易于人阅读和编写,同时也易于机器解析和生成...

    VB API 查询工具

    VB API工具通过集成API函数库和示例代码,简化了开发过程,特别是对于那些需要频繁使用系统级功能的项目。 此工具特别强调其对.NET框架的支持,这意味着不仅传统的Visual Basic 6开发者可以受益,使用VB.NET的...

    VB_API

    在“VBAPI函数参考手册.chm”这个帮助文件中,可能包含了各种API函数的详细说明,包括函数原型、参数解释、返回值、错误处理等方面的信息。这种手册对于开发者来说是非常宝贵的资源,可以帮助他们正确、安全地使用VB...

Global site tag (gtag.js) - Google Analytics