Link to home
Start Free TrialLog in
Avatar of micahjg
micahjg

asked on

Downloading From a URL

I am attempting to develop in VC++ 6.0 with MFC.  I have an array with a bunch of file names ...

001.txt
009.txt
110.txt

And I need the application to go out to a designated folder on the web and pull them down and store them to the computer's local drive ...

http://www.mysite/datafiles/001.txt   ->  c:\myfolder
http://www.mysite/datafiles/009.txt  ->  c:\myfolder
http://www.mysite/datafiles/110.txt  ->  c:\myfolder

I would like to do this all within my app and not use external software if there is a reasonable way to do it.  If someone could sketch me some code and offer suggestions on handling download failuire.  Download failure doesn't have to be elaborate ... it should just gracefully cut off the download and not place anything in the folder.



ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is also a class to download files from the net :

http://www.codeproject.com/internet/webgrab.asp <==

-MAHESH
>>Download failure doesn't have to be elaborate ... it should just gracefully cut off the download and not place anything in the folder.

You can use WinInet to download a file from the Internet. But the easier way is to use the ::URLDownloadToCacheFile() or ::URLDownloadToFile() functions. The URL functions combine the capabilities of asynchronous monikers and URL monikers into easy-to-use functions. By using these functions, you do not have to worry about the protocols used to transfer the files, such as HTTP, FTP. In the simplest case, all you have to do is to pass them the URL. ::URLDownloadToCacheFile() downloads data into the Internet cache and returns the file name of the cache location for retrieving the bits. ::URLDownloadToFile() downloads bits from the Internet and saves them to a file. However, they are blocking functions. Even though the data is downloaded asynchronously the functions do not return until all the data is downloaded. You can choose to be notified of progress through a notification callback.

Following sample demonstrates how to use the ::URLDownloadToCacheFile() function to download a file from the Internet without blocking the user interface. The use of the ::URLDownloadToFile() function is similar. The sample is an MFC dialog-based application that creates a worker thread to perform the download task.

http://www.codeproject.com/internet/urlfile.asp <===

-MAHESH