|
Here's a nice little UserControl you can drop into any VB5 or VB6 project that does just one thing, but does it very well and without complaint. You can use NetGrab to download any* file from the internet (or the local file system, for that matter) into a Byte array within your process. Also included is a SaveAs method, so you can write the resulting bytes directly to file, but only if desired.
The advantages of using AsyncRead are several. Most often, the "simple" answer to the very common problem of downloading any random file is to call the URLDownloadToFile API. However, using that call asynchronously isn't for the feint of heart. Plus, this and every other alternative introduces external dependencies, some more likely to be built-in than others.
The native capability of VB5/6 UserControls to do asynchronous downloads is seemingly an almost unknown capability. With this little demo, I hope to show you just how simple it truly can be. The included UserControl is written to just do a single download at a time. Although nothing would prevent you from using multiple instances for multiple simultaneous downloads, you could also modify this one ever so slightly to support multiple streams. The ticket would simply be in keeping a dynamic collection of keys, as demonstrated in my NetCam demo.
Note on Conditional Compilation
UserControls in VB6 offer a few extra capabilities with the AsyncRead method and events. Be sure to toggle the conditional compilation constant at the top of the CTL module if you want to use it in VB5.
* Actually, there's no provision for downloading files that require credentials. Strictly a KISS project.
This sample, or the one from which it originally derived, was published (or at least peripherally mentioned) in the following article(s):
- Simple Asynchronous Downloads, Classic VB Corner, VSM Online, March 2008
This sample uses the following API calls:
Module Library Function NetGrab.ctl kernel32 GetTickCount Don't see what you're looking for? Here's a complete API cross-reference.
Please, enjoy and learn from this sample. Include its code within your own projects, if you wish. But, in order to insure only the most recent code is available to all, I ask that you don't share the sample by any form of mass distribution. Download NetGrab.zip, 17Kb, Last Updated: Thursday, February 7, 2008
The following resources may also be of interest:
- NetCam - Provides a full-function UserControl that retrieves and displays webcam images from web.