-
Mar 7th, 2014, 05:07 PM
#1
Thread Starter
Addicted Member
[RESOLVED] How to make downloader with Progress bar in vb6 ?
Hi guys .
I actually know how to make a program downloading specific file from server , but i don't know how i can make it with Progress bar which shows remaining download percentage!
I was wondering if anybody could help me ...
-
Mar 7th, 2014, 05:16 PM
#2
Re: How to make downloader with Progress bar in vb6 ?
Please check, if the example below is of any help:
http://www.vbforums.com/showthread.p...multiple-Files
Olaf
-
Mar 8th, 2014, 02:34 AM
#3
Thread Starter
Addicted Member
Re: How to make downloader with Progress bar in vb6 ?
Thanks for giving me such an example of what i need , but it is really difficult to understand for beginner users like me!
Code of my downloader program :
- 2 textboxs ; txtfile (contains directory of file being saved in computer) , txturl (contains directory of url containing file being downloaded)
- 1 commandButton starting download procedure
- Microsoft internet transfer control with the name :"inetDownload"
Code:
Private Sub Form_Load()
Dim dir_name As String
dir_name = App.Path
If Right$(dir_name, 1) <> "\" Then dir_name = dir_name & "\"
txtFile.Text = "C:\Example.txt"
end sub
Private Sub Download_Click()
Dim bytes() As Byte
Dim fnum As Integer
txtFile.Enabled = False
txtURL.Enabled = False
Screen.MousePointer = vbHourglass
DoEvents
bytes() = inetDownload.OpenURL( _
txtURL.Text, icByteArray)
fnum = FreeFile
Open txtFile.Text For Binary Access Write As #fnum
Put #fnum, , bytes()
Close #fnum
txtFile.Enabled = True
txtURL.Enabled = True
Screen.MousePointer = vbDefault
txtFile.Text = "C:\Example.txt"
end sub
Now , would you please guide me to add a simple progress bar to this program?
Thanks
Last edited by GlowingVB; Mar 8th, 2014 at 02:39 AM.
-
Mar 8th, 2014, 09:41 AM
#4
Re: How to make downloader with Progress bar in vb6 ?
The Internet-Transfer-Control does not offer a built-in Progress-Event - that's why I posted
a Link which interacts with a normal (already VB-Runtime-provided, and thus not introducing
an Extra-OCX-dependency), VB.Usercontrol, which you can add to your StdExe-Project Privately.
Easy to re-delegate the Progress-Events of such a Project-Private Usercontrol into your
calling Form.
So why not just make your own Download-Control - you could even use it without
providing an UI (with the UCs InvisibleAtRuntime-Property set to True:
Just add a private Usercontrol into your Project, and name it ucDownload.
Then you can put the following code into it:
Code:
Option Explicit
Event DownloadProgress(ByVal BytesRead As Long, ByVal BytesTotal As Long)
Event DownloadFailed(ByVal Status As String, ByVal StatusCode As AsyncStatusCodeConstants)
Event DownloadComplete()
Public URL As String, LocalFileName As String
Public Sub DownloadFile(URL As String, LocalFileName As String, Optional ByVal Mode As AsyncReadConstants = vbAsyncReadForceUpdate)
CancelDownload
Me.LocalFileName = LocalFileName
Me.URL = URL
On Error Resume Next
AsyncRead URL, vbAsyncTypeFile, LocalFileName, Mode
If Err Then
RaiseEvent DownloadFailed(Err.Description, Err.Number)
End If
End Sub
Public Sub CancelDownload()
On Error Resume Next
CancelAsyncRead LocalFileName 'cancel a possibly still running Download with the same Destination-Filename
On Error GoTo 0
End Sub
Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)
With AsyncProp
If .BytesRead Then RaiseEvent DownloadProgress(.BytesRead, IIf(.BytesMax <= .BytesRead, .BytesRead, .BytesMax))
End With
End Sub
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
If AsyncProp.StatusCode <> vbAsyncStatusCodeEndDownloadData Or AsyncProp.BytesRead = 0 Then
RaiseEvent DownloadFailed(AsyncProp.Status, AsyncProp.StatusCode)
CancelDownload
Else
Name AsyncProp.Value As LocalFileName
RaiseEvent DownloadComplete
End If
End Sub
Now close the designer-window of ucDownload - choose it from VBs Control-Toolbar -
and place one instance of it on your Form.
Form-TestCode (needs aside from ucDownload1 also a TextBox, name txtURL)
Code:
Option Explicit
Private Sub Form_Click()
Me.Caption = "Download-starting..."
ucDownload1.DownloadFile txtURL.Text, "C:\Tests\MyDownload.zip"
End Sub
Private Sub ucDownload1_DownloadProgress(ByVal BytesRead As Long, ByVal BytesTotal As Long)
Me.Caption = Format(BytesRead / BytesTotal, "Percent")
End Sub
Private Sub ucDownload1_DownloadFailed(ByVal Status As String, ByVal StatusCode As AsyncStatusCodeConstants)
Me.Caption = "Error: " & Status
End Sub
Olaf
-
Mar 8th, 2014, 10:10 AM
#5
Re: How to make downloader with Progress bar in vb6 ?
Originally Posted by Schmidt
The Internet-Transfer-Control does not offer a built-in Progress-Event...
If somebody is using the OpenURL method the odds are high that they are probably using FTP. It seems to be the favorite of casual coders despite its many limitations.
I can't really imagine anyone using this ancient control for HTTP considering how many better choices ship as part of Windows now, even if we ignore the use of AsyncRead which is intrinsic to VB6. But you may have guessed correctly.
Even sticking with the crusty old Internet Transfer Control though you can report progress in terms of the chunks of downloaded data given to you via the StateChanged event (which of course implies the use of Execute instead of OpenURL).
-
Mar 8th, 2014, 10:48 AM
#6
Thread Starter
Addicted Member
Re: How to make downloader with Progress bar in vb6 ?
I will give it a try for sure ASAP...
-
Mar 8th, 2014, 11:48 AM
#7
Thread Starter
Addicted Member
Re: How to make downloader with Progress bar in vb6 ?
Hey guys , I think you should take a look at :
http://www.vbforums.com/showthread.p...h-Progress-Bar
IT IS POSSIBLE to do such a thing with Inet...
-
Mar 8th, 2014, 12:03 PM
#8
Re: [RESOLVED] How to make downloader with Progress bar in vb6 ?
On a Form add a CommandButton, a Progressbar, and an Inet control
Copy and Paste the following code:
Code:
Option Explicit
Private Sub cmdDownload_Click()
Screen.MousePointer = vbHourglass
ProgressBar1.Value = 0
ProgressBar1.Visible = True 'show progressbar
'This downloads the file and saves to your machine
DownloadFile "<-------- Remote file name--------->", "<---------- Local File Name -------->"
Screen.MousePointer = vbDefault
MsgBox "Download Complete"
ProgressBar1.Visible = False
End Sub
Private Sub Form_Load()
Caption = "Sample Downloader With Progress Bar"
ProgressBar1.Visible = False
End Sub
Sub DownloadProgress(intPercent As String)
ProgressBar1.Value = intPercent ' Update file download progress
End Sub
Public Sub DownloadFile(strURL As String, strDestination As String) 'As Boolean
Const CHUNK_SIZE As Long = 1024
Dim intFile As Integer
Dim lngBytesReceived As Long
Dim lngFileLength As Long
Dim strHeader As String
Dim b() As Byte
Dim i As Integer
DoEvents
With Inet1
.URL = strURL
.Execute , "GET", , "Range: bytes=" & CStr(lngBytesReceived) & "-" & vbCrLf
While .StillExecuting
DoEvents
Wend
strHeader = .GetHeader
End With
strHeader = Inet1.GetHeader("Content-Length")
lngFileLength = Val(strHeader)
Caption = lngFileLength
DoEvents
lngBytesReceived = 0
intFile = FreeFile()
Open strDestination For Binary Access Write As #intFile
Do
b = Inet1.GetChunk(CHUNK_SIZE, icByteArray)
Put #intFile, , b
lngBytesReceived = lngBytesReceived + UBound(b, 1) + 1
'On Error Resume Next
DownloadProgress (Round((lngBytesReceived / lngFileLength) * 100))
DoEvents
Loop While UBound(b, 1) > 0
Close #intFile
End Sub
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|