Downloading From a URL

Posted on 2006-05-22
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
    LVL 30

    Accepted Solution

    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

    LVL 22

    Expert Comment

    Here is also a class to download files from the net : <==

    LVL 22

    Expert Comment

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


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
    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.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now