Jessee
asked on
VB6 Downloading file with progress bar and KBP/S
Hey guys,
Im needing to know how to download a file with a progress bar and showing the KBP/S.
The program will be the same every time and the URL will not change.
The code I have is below and it works fine. Just need to get the Progress bar and the KBP/S implemented.
Thanks,
Jessee
Im needing to know how to download a file with a progress bar and showing the KBP/S.
The program will be the same every time and the URL will not change.
The code I have is below and it works fine. Just need to get the Progress bar and the KBP/S implemented.
Thanks,
Jessee
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Public Function DownloadFromUrl(strFullUrl As String, _
strSaveFile As String) As Boolean
Dim RetVal As Long
RetVal = URLDownloadToFile(0, strFullUrl, strSaveFile, 0, 0)
'Operation succeeded
If RetVal = 0 Then
DownloadFromUrl = True
End If
End Function
sorry that link again
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=64914&lngWId=1
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=64914&lngWId=1
You might need to determine if your server returns the content-length header of the file in bytes, otherwise you won't know the size of the file to calculate the progress.
You can use the following code to check if it returns the size of the requested file
You can use the following code to check if it returns the size of the requested file
Option Explicit
Private Const WININET_API_FLAG_SYNC = &H4&
Private Const HTTP_QUERY_CONTENT_LENGTH = &H5&
Private Const HTTP_QUERY_FLAG_NUMBER = &H20000000
Private Declare Function InternetOpenW Lib "wininet" (ByVal lpszAgent As Long, ByVal dwAccessType As Long, ByVal lpszProxyName As Long, ByVal lpszProxyBypass As Long, ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrlW Lib "wininet" (ByVal hInternet As Long, ByVal lpszUrl As Long, ByVal lpszHeaders As Long, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInternet As Long) As Long
Private Declare Function HttpQueryInfoW Lib "wininet.dll" (ByVal hRequest As Long, ByVal dwInfoLevel As Long, ByRef lpvBuffer As Long, ByRef lpdwBufferLength As Long, ByRef lpdwIndex As Long) As Long
Public Function GetContentLength(ByVal url As String) As Long
Dim hInternet As Long
Dim hRequest As Long
Dim dwFileSize As Long
Dim dwLength As Long
Dim dwIndex As Long
dwLength = 4 ' DWORD 32 bit value buffer length.
hInternet = InternetOpenW(0, 1, 0, 0, WININET_API_FLAG_SYNC)
hRequest = InternetOpenUrlW(hInternet, StrPtr(url), 0, 0, 0, 0)
If HttpQueryInfoW(hRequest, HTTP_QUERY_CONTENT_LENGTH Or HTTP_QUERY_FLAG_NUMBER, _
dwFileSize, dwLength, dwIndex) Then
GetContentLength = dwFileSize
Else
GetContentLength = (-1)
End If
InternetCloseHandle hRequest
InternetCloseHandle hInternet
End Function
Private Sub Form_Load()
Debug.Print GetContentLength("http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif")
End Sub
ASKER
Hey,
It does return the correct size.
What next?
Thanks,
Jessee
It does return the correct size.
What next?
Thanks,
Jessee
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hey,
How do I change it to Progress bar format?
I tried:
frmDownload.PgbProgress.Ma x = dwFileSize
frmDownload.PgbProgress.Va lue = dwBytesWritten
How do I change it to Progress bar format?
I tried:
frmDownload.PgbProgress.Ma
frmDownload.PgbProgress.Va
The progress bar values should be in the range 0-100 as the example calculates the percentage.
frmDownload.PgbProgress.Value = dwPercent
ASKER
Hey,
Sweet. Im now getting a CreateFile error 32?
Sweet. Im now getting a CreateFile error 32?
The file handle is still opened usually happens when your debugging and the IDE throws an error and the CloseHandle() never had a chance to cleanup the handle. You have to close out the IDE or application then start fresh.
ASKER
Hey,
So if I was running this as a .exe it wouldnt come up with this error?
Just restarted vb6 and it fixed it :)
Do you know of a way to find out the KBP/S?
So if I was running this as a .exe it wouldnt come up with this error?
Just restarted vb6 and it fixed it :)
Do you know of a way to find out the KBP/S?
What happens is that in the IDE if you would click the Stop or a Debugging error occured before the download completed the executing code would never have cleaned up the opened file handle. You can extend the example by adding a cancel flag that exits the loop if it's in progress so that the call to CloseHandle() will always cleanup the handle then you won't have to be concerned with this error, you also have to make sure that the location you download has write access permissions.
ASKER
Ah I see what you mean now.
Are you able to help with the KBPS?
Are you able to help with the KBPS?
Calculate how many bytes have been read every second and divide by 1000. You then might need to take the overhead of time of the WriteFile() call to get the closest result.
Disable the timer and make the Interval 1000, just before the Loop begins enable the timer and then after loop disable the timer.
If your working with a small file I wouldn't even bother calculating the transfer rate , just show how many bytes have been completed of the file instead.
Disable the timer and make the Interval 1000, just before the Loop begins enable the timer and then after loop disable the timer.
If your working with a small file I wouldn't even bother calculating the transfer rate , just show how many bytes have been completed of the file instead.
ASKER
Thanks :)
A URLDownloadToFile Demo WITH PROGRESS bar/CANCEL