QB64 Phoenix Edition
Grab URL from net to file - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: Grab URL from net to file (/showthread.php?tid=2124)



Grab URL from net to file - eoredson - 10-28-2023

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.


RE: Grab URL from net to file - DSMan195276 - 10-28-2023

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).


RE: Grab URL from net to file - eoredson - 10-28-2023

Ok, thanks.

$Unstable:Http

have to be used?


RE: Grab URL from net to file - SpriggsySpriggs - 10-28-2023

Check my programming corner for 3 download routines


RE: Grab URL from net to file - eoredson - 10-31-2023

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

Where is your programming corner?


RE: Grab URL from net to file - TerryRitchie - 10-31-2023

(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


RE: Grab URL from net to file - eoredson - 11-01-2023

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..


RE: Grab URL from net to file - SpriggsySpriggs - 11-01-2023

(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.