Downloading From a URL

Posted on 2006-05-22
Medium Priority
Last Modified: 2013-11-20
I am attempting to develop in VC++ 6.0 with MFC.  I have an array with a bunch of file names ...


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.

Question by:micahjg
  • 3
LVL 30

Accepted Solution

Axter earned 2000 total points
ID: 16739760
You can use URLDownloadToFile.
#include <Urlmon.h>
URLDownloadToFile ( NULL, "http://www.mysite/datafiles/001.txt  ", "c:\\myfolder\001.txt", 0, NULL);
LVL 22

Expert Comment

ID: 16739964
LVL 22

Expert Comment

ID: 16740002
Here is also a class to download files from the net :

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

LVL 22

Expert Comment

ID: 16740080
>>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 <===


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question