Click here to Skip to main content
Click here to Skip to main content

Download a File Using URLDownloadToCacheFile

By , 25 Oct 2002
 

Download

Introduction

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.

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

The user is supposed to enter the URL in the URL edit box and then press the Download button. The CUrlFileDlg::OnOK() will be called since the ID of the Download button is IDOK. The CUrlFileDlg::OnOK() retrieves the URL string from the URL edit box and checks its validity using the ::IsValidURL() function. The second parameter of the ::IsValidURL() function is expected to be of the LPCWSTR type. Here, the T2CW conversion macro is used. For more information about MBCS/Unicode Conversion Macros, see the MFC Technical Note "TN059: Using MFC MBCS/Unicode Conversion Macros". If the URL is valid (but it is not necessarily correct), a worker thread is created by calling ::AfxBeginThread(). The controlling function for the worker thread is a static member function of CUrlFileDlg - CUrlFileDlg::Download(), which calls ::URLDownloadToCacheFile() and posts a user-defined message WM_USER_ENDDOWNLOAD to the dialog box after ::URLDownloadToCacheFile() returns. The message-handler function for WM_USER_ENDDOWNLOAD waits until the worker thread terminates, then deletes the CWinThread object. It also displays the name of the downloaded file. The last parameter of ::URLDownloadToCacheFile() is the address of the caller's IBindStatusCallback interface. ::URLDownloadToCacheFile() calls this interface's IBindStatusCallback::OnProgress() method on a connection activity, including the arrival of data. Implementing IBindStatusCallback::OnProgress() allows a caller to implement a user interface or other progress monitoring functionality. It also allows the download operation to be canceled by returning E_ABORT from the IBindStatusCallback::OnProgress() call. The implementation of the IBindStatusCallback interface is the CBSCallbackImpl class. The CBSCallbackImpl::OnProgress() sends a user-defined message WM_USER_DISPLAYSTATUS to the dialog box to display the progress messages. It also uses the ::WaitForSingleObject() function to check the current state of the event object, which is set to the signaled state when the user has pressed the Stop button (the same button as the Download button) during downloading. If the event object is in the signaled state, the CBSCallbackImpl::OnProgress() returns E_ABORT to cancel the download operation.

Note that the following setting affects the behavior of ::URLDownloadToCacheFile() just like the Internet Explorer browser.

Check Control Panel/Internet/General/Temporary Internet files/Settings/Check for newer versions of stored pages

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Chen Su
Software Developer
Canada Canada
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1membersujit_haldar7 Sep '12 - 4:54 
vccxvxc
GeneralMy vote of 5 [modified]memberprojectzombie8 Dec '11 - 16:47 
Being inspired by your code I built an online version of how to Download a file from a URL using C# for image manipulation. I did not use the ::URLDownloadToCacheFile() or ::URLDownloadToFile() functions. But your idea is a powerful one. My version of this article can be viewed at http://docx.us/.

modified 28 Mar '12 - 13:04.

GeneralRe: My vote of 5memberChen Su8 Dec '11 - 17:05 
Cool! Thanks!
QuestionCould have been more simple...membervantomas12 Aug '11 - 4:05 
Instead of implementing a whole dialog box a simple download class in a seperated unit could have been better, cos its confusing what happens.
GeneralDelete Downloaded FilememberRobairbutler10 Jun '10 - 22:58 
How can I delete the file after it is downloaded using code?
 
Thanks
 
Rob
GeneralRe: Delete Downloaded FilememberChen Su11 Jun '10 - 14:46 
::DeleteUrlCacheEntry() for a cached file
 
or CFile::Remove() / ::DeleteFile() for a regular file
GeneralRe: Delete Downloaded FilememberRobairbutler14 Jun '10 - 1:42 
::DeleteUrlCacheEntry(pDownloadParam->strURL);
 

Worked great thanks
 
R
QuestionPause / Resumememberkhoanguyen11 Dec '09 - 18:02 
Hi all!
 
I can't resume when i try to download this url: http://netcafe7.24h.com.vn/gamesonline/fifaonline2_install_151109.zip (must be start at beginning)
 
but with this link ftp://dlfptsg.ge.gate.vn/vGE_2.9.1.95794-7.bin, it's ok.
 
Please help me!
QuestionChange download directorymemberawelch23 Oct '09 - 5:46 
Hello. This example is just what i have been looking for, however i need to be able to change the directory where the file is saved. Is there a variable somewhere that i can change to save the file to say the "C" Drive somewhere?
 
Thanks,
Adam
AnswerRe: Change download directorymemberChen Su23 Oct '09 - 11:46 
As my article mentions, use the ::URLDownloadToFile() function instead which allows you to specify the full path name.
QuestionHow to block IE generate temporary files?memberXuefei.Wu13 Apr '09 - 17:09 
Do you know how to block IE generate temporary files?
GeneralGood code, not very good article:)memberzengkun10012 Jun '08 - 1:04 
Big Grin | :-D
 
A Chinese VC++ programmer

Generalpause and resumemembermonsieur_jj7 May '08 - 20:14 
Hi,
 
Is there away to have a pause and resume download with UrlDownloadtoCacheFile, lets say I am downloading a zip file thats 500mb then i am disconnected to the internet i stopped at 200mb can i resume to that point? Or how can i resume to that point without the use of MFC?
AnswerRe: pause and resumememberChen Su8 May '08 - 9:14 
UrlDownloadtoCacheFile() doesn't support pause and resume.
QuestionURLDownloadToCacheFile in Asynchronous modemembercall4e_friend21 May '07 - 21:56 
I am using URLDownloadToCacheFile to download the url.
It is working fine and it is also showing progress bar very properly.
 
But I want to perform this process asynchronously. URLDownloadToCacheFile function should return immediately and should execute following statement. Following statement may be another call to URLDownloadToCacheFile function to start another file for download. I want to download multiple files at a time (like multithreading)
 
To do so :-
- I tried handling pbindinfo parameter in GetBindInfo.
- I set grfBINDF = BINDF_ASYNCHRONOUS
- I passed BINDF_ASYNCHRONOUS argument to URLDownloadToCacheFile function.
And much more. Nothing works.
 
Can anyone please provide me some source code (in Visual Basic 6 or any other development language) that downloads url asynchronously ?
 
Thanks in advance.
 
Amit Joshi
AnswerRe: URLDownloadToCacheFile in Asynchronous modememberChen Su22 May '07 - 19:15 
As I mentioned in the article, the URLDownloadToCacheFile() function is a blocking function. To do what you want, you need to create a worker thread, which is exactly what my sample demonstrates.
QuestionFile convertionmemberkalaikumar.t10 Jul '06 - 23:53 
how to convert powerpoint file to flash file using mfc
GeneralCommandline with UrlmemberRhettCawood10 Jul '06 - 19:50 
Is there any way to open your Downloader.exe with a commandline containing the URL I want to Download from - Appreciate your help#

 
Rhett Cawood
GeneralRe: Commandline with UrlmemberChen Su13 Jul '06 - 13:56 
You will have to make some changes. It should be easy to do. Instead of getting the URL from the edit box, get it from the command line.
GeneralRe: Commandline with UrlmemberRhettCawood14 Jul '06 - 4:18 
I have never programed in C however Am over last few days trying to pick up basics - Please could you give me idea in your code as to where to pick up that you apply the edit box text as the url and where the command line [VBDemo.exe / "http://www.exampleurl.com/test.zip"] would be picked up?
 
Rhett Cawood
GeneralVB Version of this downloadermemberRhettCawood10 Jul '06 - 14:09 
Is there any way you could create a sample of this URL Downloader to be used in an Access Module
 
Rhett Cawood
GeneralRe: VB Version of this downloadermemberChen Su10 Jul '06 - 16:32 
Sorry, I have never used VB. Frown | :-(
Questionit do not work on my computermemberr0dmetal22 Mar '06 - 8:05 
Hello, i tried your sample on my computer and it do not work. I use visual styudio 2003 on windows XP. I tried with several URLs, but it never worked.
This is a print screen of the error:
http://img452.imageshack.us/img452/7845/printscreen19it.th.jpg
This is hr value after the call of URLDownloadToCacheFile() function:
hr: 0x800401e4 Syntaxe incorrecte (incorrect syntax)
 
Thanks for help Smile | :)

GeneralAuthentication and URLDownloadToCacheFilememberJohnFx7 Mar '06 - 6:29 
I have code working using the URLDownloadToCacheFile method, but it seems to fail (with the super informative E_FAIL code) when I give it the URL of any site that requires basic authentication Integrated and Anonymous seem to work fine. I didn't see any UI for passing credentials so I assumed it would either prompt me or use the cached browser credentials.
 

Is anyone else here using this against a site that requires a login?

 
Confused | :confused: Confused | :confused: Confused | :confused:
GeneralCheck the status Wether Connect to the internet or notmemberKutti Ra30 Sep '05 - 16:07 
Weather check status system connect to the internet or not, is yes duration of connected times.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 26 Oct 2002
Article Copyright 2002 by Chen Su
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid