Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grab URL from net to file
#1
I have recently been using the following function to grab a file from the net:

Code: (Select All)
Rem Program to grab file from url PD 2023.

Declare Dynamic Library "urlmon"
  Function URLDownloadToFileA% (ByVal pCaller As Long, szURL As String, szFileName As String, Byval dwReserved As Long, Byval lpfnCB As Long)
End Declare

Declare Dynamic Library "kernel32"
  Function GetLastError& ()
  Function FormatMessageA& (ByVal f As Long, f$, Byval e As Long, Byval d As Long, g$, Byval s As Long, h$)
End Declare
Dim Shared ErrorBuffer As String * 260
Call GrabURL
End

' expiremental url grab.
Sub GrabURL
  ' URL to grab (page or a file)
  URL$ = "http://www.filegate.net/pub/oredson/emc3.zip"

  ' File to save URL as
  URLfile$ = "emc3.zip"

  ' display files
  Print "URL: "; URL$
  Print "File: "; URLfile$

  ' Download it.  Returns 0 if started.
  a& = URLDownloadToFileA%(0, URL$ + Chr$(0), URLfile$ + Chr$(0), 0, 0)
  If a& = 0& Then
      Print "Download started."
  End If
  If a& > 0& Then
      y& = FormatMessageA&(&H1200, "", a&, 0, ErrorBuffer$, 260, "")
      Var1$ = "Error 0x" + Hex$(a&): Print Var1$
      If y& > 2 Then
        Var2$ = Left$(ErrorBuffer$, y& - 2): Print Var2$
      End If
  End If
End Sub
but what I want to know if there is a way to determine the progress of the download if it is a very large file!?

Thanks, Erik.
Reply
#2
You can get progress information by providing an implementation of the `IBindStatusCallback` interface to the `lpfnCB` parameter. This can't be done via QB64 alone though, you'd need to write some C++ code to implement the interface and then you could pass the progress information back to QB64.

That said, recent QB64-PE versions have native HTTP support which you could use in place of `URLDownloadToFile` and would allow you to display progress information. See Examples 2 and 4 on the Wiki page about it, they show examples of displaying the progress of the download as it is happening, and also downloading directly to a file (where you write to the file as you receive the data).
Reply
#3
Ok, thanks.

$Unstable:Http

have to be used?
Reply
#4
Check my programming corner for 3 download routines
Tread on those who tread on you

Reply
#5
(10-28-2023, 12:06 PM)SpriggsySpriggs Wrote: Check my programming corner for 3 download routines

Where is your programming corner?
Reply
#6
(10-31-2023, 07:22 AM)eoredson Wrote:
(10-28-2023, 12:06 PM)SpriggsySpriggs Wrote: Check my programming corner for 3 download routines

Where is your programming corner?

https://qb64phoenix.com/forum/forumdisplay.php?fid=30
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#7
Hmm.. A GrabUrl that actually works!?

Could you add Ctrl-V to paste url?

Thanks, Erik.

btw: when input as invalid url such as: splat.txt it throws multiple dialog error boxes..
Reply
#8
(11-01-2023, 05:44 AM)eoredson Wrote: Hmm.. A GrabUrl that actually works!?

Could you add Ctrl-V to paste url?

Thanks, Erik.

btw: when input as invalid url such as: splat.txt it throws multiple dialog error boxes..
The initial screen should be a console window, which allows for pasting. As far as the errors go, I did not do any error checking for invalid URLs because these are primarily examples for showing how to do it rather than finished products. You would have to write your own error handlers. But all three do work and work well, at least.
Tread on those who tread on you

Reply




Users browsing this thread: 1 Guest(s)