当前位置: 源码素材网 » VB教程 » 详情页

VB通过网络下载文件,显示进度条

  •   时间:2018-11-22
  • 概述:下载文件

Vb网络下载程序代码,下载过程中适时显示进度条,请确保要下载文件的URL是正确的,否则可能会出错,这是我收藏的代码,如下所示:

VERSION 5.00
Object = "{48E59290-9880-11CF-9754-00AA00C00908}#1.0#0"; "MSINET.OCX"
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.3#0"; "COMCTL32.OCX"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form Form1
   Caption         =   "网络文件下载"
   ClientHeight    =   1920
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6300
   LinkTopic       =   "Form1"
   ScaleHeight     =   1920
   ScaleWidth      =   6300
   StartUpPosition =   3  '窗口缺省
   Begin MSComDlg.CommonDialog CommonDialog1
      Left            =   570
      Top             =   1980
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin VB.CommandButton Command2
      Caption         =   "<<"
      Height          =   345
      Left            =   5925
      TabIndex        =   6
      Top             =   630
      Width           =   375
   End
   Begin VB.TextBox Text2
      Height          =   345
      Left            =   1605
      TabIndex        =   3
      Text            =   "Text2"
      Top             =   630
      Width           =   4365
   End
   Begin ComctlLib.ProgressBar ProgressBar1
      Height          =   225
      Left            =   60
      TabIndex        =   2
      Top             =   1470
      Width           =   4470
      _ExtentX        =   7885
      _ExtentY        =   397
      _Version        =   327682
      Appearance      =   1
   End
   Begin InetCtlsObjects.Inet Inet1
      Left            =   1950
      Top             =   2100
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
   End
   Begin VB.CommandButton Command1
      Caption         =   "开始下载"
      Height          =   405
      Left            =   4785
      TabIndex        =   1
      Top             =   1410
      Width           =   1395
   End
   Begin VB.TextBox Text1
      Height          =   345
      Left            =   1605
      TabIndex        =   0
      Top             =   75
      Width           =   4665
   End
   Begin VB.Label Label2
      Caption         =   "下 载 到:"
      Height          =   300
      Left            =   15
      TabIndex        =   5
      Top             =   645
      Width           =   1545
   End
   Begin VB.Label Label1
      Caption         =   "网络文件所在地址:"
      Height          =   285
      Left            =   15
      TabIndex        =   4
      Top             =   150
      Width           =   1650
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_Click()
    Dim sp As String
    Dim filename$
    On Error GoTo X
    sp = InStrRev(Text1.Text, "/")
    filename = Right(Text1.Text, Len(Text1.Text) - sp) '获取文件名
    If CommonDialog1.filename = "" Or CommonDialog1.filename = filename Then
        Text2.Text = App.Path & "\" & filename
    End If
    Inet1.Execute Text1.Text, "get"    '开始下载
    ProgressBar1.Value = 0
    ProgressBar1.Visible = True
    Exit Sub
X:
    If Err.Description <> "" Then
        MsgBox Err.Description, , "信息提示"
    Else
        MsgBox "下载过程中出现错误", , "信息提示"
    End If
End Sub
Private Sub Command2_Click()
    Dim sp As String
    Dim filename$
    sp = InStrRev(Text1.Text, "/")
    filename = Right(Text1.Text, Len(Text1.Text) - sp) '获取文件扩展名
    CommonDialog1.Filter = "(*.*)|*.*"
    CommonDialog1.filename = filename
    CommonDialog1.ShowSave
    If CommonDialog1.filename <> filename Then Text2.Text = CommonDialog1.filename
End Sub
Private Sub Form_Load()
    Text1.Text = "http://192.168.1.125/111.doc"
    Text2.Text = App.Path
    ProgressBar1.Visible = False
End Sub
Private Sub Inet1_StateChanged(ByVal State As Integer)
    Dim bData() As Byte
    Select Case State
    Case icError '11
        bData = Inet1.ResponseCode & ":" & Inet1.ResponseInfo
    Case icResponseCompleted ' 12
        Dim bDone As Boolean: bDone = False
        bData() = Inet1.GetChunk(1024, 1)
        DoEvents
        Open Text2.Text For Binary Access Write As #1
        If Len(Inet1.GetHeader("Content-Length")) > 0 Then ProgressBar1.Max = CLng(Inet1.GetHeader("Content-Length"))
        '循环分块下载
        Do While Not bDone
            Put #1, Loc(1) + 1, bData()
            bData() = Inet1.GetChunk(1024, 1)
            DoEvents
            ProgressBar1.Value = Loc(1)   '设置进度条长度
            If Loc(1) >= ProgressBar1.Max Then bDone = True
        Loop
        Close #1
        MsgBox "下载完成", vbInformation, "信息提示"
    End Select
End Sub

    相关声明:

      若“VB通过网络下载文件,显示进度条”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。