PDA

View Full Version : Progress Bar for Download



Movian
12-05-2008, 11:54 AM
Hey,
i found the following code that allows for the download of a file in the background using Winhttp. I have a button on a form that begins the process. What i would LIKE to do is setup a progress bar (two rectangles, 1 blue and i just gradually increase the width). However i am at a loss how to keep track of the download speed to convert to a width increment... any suggestions as always are GREATLY appreciated.

Dim FileNum As Long
Dim FileData() As Byte
Dim MyFile As String
Dim WHTTP As Object

On Error Resume Next
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
If Err.Number <> 0 Then
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
End If
On Error GoTo 0

MyFile = "http://www.exampleURL.com/Upgrade.exe"

WHTTP.Open "GET", MyFile, False
WHTTP.Send
FileData = WHTTP.ResponseBody
Set WHTTP = Nothing

FileNum = FreeFile
Open CurrentProject.path & "\Upgrade.exe" For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum

MsgBox "Download Complete"
ShellExecute 1, "open", CurrentProject.path & "\Upgrade.exe", "", CurrentProject.path, "1"
DoCmd.Quit acQuitSaveAll

For those who are interested this is in conjunction with my previous question regarding table updating. I have a mysql database on the web that stores the current version number. When we release an update we increment that number and Our program checks the number and if it is lower than the current number prompts the user that a new version is available. Then it asks if they wish to download it, if they select yes it activates this sub and downloads the upgrade.exe (a self extracting silent rar file that automaticly launches and upgrade.accdb file) runs the file and quits the main database. This progress bar would let the user know how long it will take to download the update as it can be up to 50 MB and on slower connections that can take a while.

Movian
12-08-2008, 06:48 AM
after much looking around and not much progress i found a VB program that seemed to have the answer. It used some preset subs to perform iperations, i am presuming they are accessd by the http .dll

however they seem to be working but not at the same time.

The file gets downloaded and apears to work correctly. however i am unable to adjust the width of the progress bar on my form upgrade. Nor am i able to put up a simple message box from one of these subs. However as i said they seem to be making the file correctly, in the right location with the right name etc. Any further thoughts ?



Public Sub DownloadUpgrade()
Dim FileNum As Long
Dim FileData() As Byte
Dim MyFile As String
Dim MyCon As Object

On Error Resume Next
Set MyCon = CreateObject("WinHTTP.WinHTTPrequest.5")
If Err.Number <> 0 Then
Set MyCon = CreateObject("WinHTTP.WinHTTPrequest.5.1")
End If
On Error GoTo 0

MyFile = "http://www.somesite.com/downloads/Upgrade.exe"

MyCon.Open "GET", MyFile, False
MyCon.Send
FileData = MyCon.ResponseBody
Set MyCon = Nothing

FileNum = FreeFile
Open CurrentProject.path & "\Upgrade.exe" For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum


End Sub
Private Sub http_OnResponseDataAvailable(Data() As Byte)
mprogress = mprogress + UBound(Data) + 1
Call Forms("Upgrade").UpdateProgress(mprogress)
Put #1, , Data
End Sub

Private Sub http_OnResponseFinished()
Close #1
Screen.Application.Echo True
MsgBox "done"
ShellExecute 1, "open", CurrentProject.path & "\Upgrade.exe", "", CurrentProject.path, "1"
DoCmd.Quit acQuitSaveAll
End Sub
Private Sub http_OnResponseStart(ByVal Status As Long, ByVal ContentType As String)
'Screen.Application.Echo False
MsgBox "test"
Open CurrentProject.path & "\Upgrade.exe" For Binary As #1
End Sub