Liberty BASIC Default Variables
Internet - Downloading a File to Disk
API Corner - Easy Font Manipulations
Mapping Real World Coordinates
Download from the Internet
It is easy to download a file from the internet to your hard drive. The API call URLDownloadToFileA will do it quite easily. It requires a valid URL to a file on the internet, and a filename for saving this file on disk.
calldll #url, "URLDownloadToFileA",_ 0 as long,_ 'null urlfile$ as ptr,_ 'url to download localfile$ as ptr,_ 'save file name 0 as long,_ 'reserved, must be 0 0 as long,_ 'callback address, can be 0 ret as long
The first argument is reserved for languages that have access to COM objects and ActiveX controls. Since Liberty BASIC doesn't have this functionality, the first argument is passed as 0.
The second argument (urlfile$) is a string containing the URL of the file to download. This function downloads a single file. If you choose to download an html file (a webpage) then you will get only that file. You won't get images or other files that are contained on the website. You must download each of these files separately.
The third argument contains the filename under which to save the downloaded file. It is the responsibility of the programmer to determine if a file with this name already exists on disk and offer the user the chance to select a new name or cancel the operation. Use the FILES command to determine if a file exists. (See the LB helpfile for more on using the FILES command.)
The fourth argument is reserved and must be passed as 0.
The fifth argument is a callback function address. This feature is not used in the demo below. It could be used to monitor the progress of the download and give the user updates on the progress or allow the user to cancel the operation.
The return value is 0 if the function succeeds.
Demo
The following demo downloads the Liberty BASIC main html page and it also downloads the banner image from the page. If the html download is successful, the demo opens the file, reads it, and prints the contents to the mainwindow. If the banner download is successful, the banner is displayed in MS Paint.
'LB3+ Download html from given 'URL to file on disk. ' 'Minimum availability Internet Explorer 3.0 'Minimum operating systems Windows NT 4.0, Windows 95 ' 'If an html page is requested, only the html is saved, 'not images. Download images separately as below. S.OK=0 open "URLmon" for dll as #url 'first, get LB webpage html: urlfile$="http://www.libertybasic.com/" filedialog "Save URL as...","*.html",localfile$ if localfile$="" then [quit] calldll #url, "URLDownloadToFileA",_ 0 as long,_ 'null urlfile$ as ptr,_ 'url to download localfile$ as ptr,_ 'save file name 0 as long,_ 'reserved, must be 0 0 as long,_ 'callback address, can be 0 ret as long 'check if file downloaded and print in mainwindow: if ret<>S.OK then print "Unsuccessful." else open localfile$ for input as #f txt$=input$(#f,lof(#f)) print trim$(txt$) close #f end if print : print 'now download the banner: urlfile$="http://www.libertybasic.com/lb3banner.jpg" filedialog "Save URL as...","*.jpg",localfile$ if localfile$="" then [quit] calldll #url, "URLDownloadToFileA",_ 0 as long,_ 'null urlfile$ as ptr,_ 'url to download localfile$ as ptr,_ 'save file name 0 as long,_ 'reserved, must be 0 0 as long,_ 'callback address, can be 0 ret as long if ret<>S.OK then print "Unsuccessful." else run "mspaint.exe " + chr$(34) + localfile$ + chr$(34) end if print : print : print "Done" [quit] close #url:end