Chinaunix首页 | 论坛 | 博客
  • 博客访问: 574349
  • 博文数量: 190
  • 博客积分: 10937
  • 博客等级: 上将
  • 技术积分: 2205
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-07 11:28
文章分类

全部博文(190)

文章存档

2012年(1)

2011年(27)

2010年(20)

2009年(142)

我的朋友

分类:

2009-04-13 10:26:11

'添加 internet transfer control 6.0 和 windows commom controls 6.0
'form代码:

Private Sub cmdGET_Click()
StartDownLoad txtURL
End Sub

Private Sub Form_Load()
savefile.Text = App.Path
End Sub

Private Sub StartDownLoad(ByVal Geturl As String)
Dim spo%, filename$
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(App.Path & "\download") Then Set f = fso.CreateFolder(App.Path & "\download")
spo = InStrRev(Geturl, "/")
filename = Right(Geturl, Len(Geturl) - spo) '获取文件名
savefile.Text = App.Path & "\download\" & filename
Inet1.Execute Geturl, "get"   '开始下载
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
   'State = 12 时,用 GetChunk 方法检索服务器的响应。
   Dim vtData() As Byte
   Select Case State
   '...没有列举其它情况。
   Case icError '11
      '出现错误时,返回 ResponseCode 和 ResponseInfo。
      vtData = Inet1.ResponseCode & ":" & Inet1.ResponseInfo
   Case icResponseCompleted ' 12
      Dim bDone As Boolean: bDone = False
      '取得第一个块。
      vtData() = Inet1.GetChunk(1024, 1)
      DoEvents
      Open savefile.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, vtData()
         vtData() = 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

阅读(1699) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~