How can I get Progress of downloaded files in URLDownloadToFile API?
Printable View
How can I get Progress of downloaded files in URLDownloadToFile API?
That program maybe OK as long as the file has a Content-Length associated with it but .HTML files do not.
EDIT AFTER POST:
I ran that program several times using different files to download. It downloads .TXT files OK and .HTML files OK (but no progressbar because .HTML files have no Content-Length). It does not download .EXE, .ZIP files correctly. The downloaded files are much smaller in size and are corrupted. If the file has no Content-Length (as in .HTML) you will get an error when it tries to move the progressbar because the file length is 0 and you will get a division by zero error.
Download with progress can be achieved easily with the WinHTTP library in asynchronous mode. Add reference to WinHTTP.dll in systems32. (Browse to it, if Microsoft WinHTTP services doesn't show on the references list.)
Note this simple example doesn't do anything fancy, just dumps the downloaded bytes to disk.
OnResponseStart when you can safely access the headers.
OnResponseDataAvailable which returns a byte array of downloaded data for each chunk.
OnResponseEnd which ends the download.
Code:Option Explicit
Private WithEvents http As WinHttpRequest
Private mContentLength As Long
Private mProgress As Long
Private Sub Command1_Click()
' Create the WinHTTPRequest ActiveX Object.
Set http = New WinHttpRequest
' Open an HTTP connection.
http.open "GET", "http://home.in.tum.de/~paula/mpeg/wg_gdo_1.mpg", True 'True means asynch.
' Send the HTTP Request.
http.send
End Sub
Private Sub http_OnResponseDataAvailable(Data() As Byte)
mProgress = mProgress + UBound(Data) + 1
ProgressBar1.Value = mProgress
Put #1, , Data
End Sub
Private Sub http_OnResponseFinished()
Close #1
MsgBox "done"
End Sub
Private Sub http_OnResponseStart(ByVal Status As Long, ByVal ContentType As String)
Text1.Text = http.getAllResponseHeaders()
mProgress = 0
mContentLength = CLng(http.getResponseHeader("Content-Length"))
ProgressBar1.Max = mContentLength
Open "C:\twg_gdo_1.mpg" For Binary As #1
End Sub
Same situation. You are depending on Content-Length just like the first program.
With AJAX and Iframes how can you tell the Content length of html anyways?
Don't know what AJAX is but I wouldn't think that the content length of an HTML file has anything to do with Iframes.
I follow Microsofts example when setting up a progress bar for downloading files.
I don't waste time with meaningless programming code trying to figure out what I should set as the max on my progress bar....I, like Microsoft, simply pull a number out of my hat and use that.
(PS: I'm not kidding...that is exactly what I do, and I'm convinced that is what Microsoft and others do as well.)
is this what you want?
Code:Private Declare Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String) As Long
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
Dim retVal As Long
Dim theUrl As String
Dim savePath As String
Dim pathExist As Long
that url doesnt excist.Code:Private Sub Command1_Click()
theUrl = "http://www.w3schools.com/index.html"
If theUrl = "" Then Exit Sub
savePath = App.Path + "/w3index.html"
If savePath = "" Then Exit Sub
retVal = URLDownloadToFile(0, theUrl, savePath, 0, 0)
If retVal = 0 Then
msgbox "success"
Else
msgbox "failed"
End If
End Sub
Are you sure you are posting in the correct thread?Quote:
Originally Posted by Justa Lol
Iframes us multiple clients to download nested content as for AJAX that's active content http://www.w3schools.com/Ajax/Default.AspQuote:
Originally Posted by jmsrickland
That still has nothing to do with the length of an HTML file.
Yes the length of the parent file is the same but the entire screen content is much larger since it is a hierarchy of html files. And with Ajax the content size since the internal html will change dynamically. Usually when you Iframe or use frames in general you root html is miniscule.
That's my point. When you download an HTML file you are looking for the length of that file as it is in it's text state. What the internal elements do later (like make it bigger on the screen) is not relavent to it's original file length. That's why I stated I wouldn't think that the content length of an HTML file has anything to do with Iframes because it doesn't. The browser downloads the original HTML document then downloads the HTML that makes up the iFrame. Two different files.
My point is: Whats the ultimate goal of tracking the progress on a bar. the 2seconds it takes to download the parent, or an indicator that all of the content has arrived?
No, no, no. I think you are missing the point. The OP is not trying to do like a browser does and download all that is needed. The original question was
How can I get Progress of downloaded files in URLDownloadToFile API which suggests that he will download one file at a time with a progressbar for each downloaded file. But the browser downloads the main HTML and then begins to download all the images, iframe documents, etc, etc which the browser will include these in one progressbar download. These are two different things here. If this is not what the OP was suggesting then his 1st post is a little misleading then.