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 ...:bigyello:
Printable View
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 ...:bigyello:
Please check, if the example below is of any help:
http://www.vbforums.com/showthread.p...multiple-Files
Olaf
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"
Now , would you please guide me to add a simple progress bar to this program?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
Thanks
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:
Now close the designer-window of ucDownload - choose it from VBs Control-Toolbar -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
and place one instance of it on your Form.
Form-TestCode (needs aside from ucDownload1 also a TextBox, name txtURL)
OlafCode: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
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).
I will give it a try for sure ASAP...
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...:rolleyes:
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