Click here to Skip to main content
11,708,312 members (69,501 online)
Click here to Skip to main content

Download a File Using URLDownloadToCacheFile

, 25 Oct 2002 149.1K 6.6K 66
Rate this:
Please Sign up or sign in to vote.
A sample to demonstrate how to use URLDownloadToCacheFile and IBindStatusCallback.

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

Share

About the Author

Chen Su
Software Developer
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionReally beautiful code Pin
Michael B Pliam22-Jul-13 12:32
memberMichael B Pliam22-Jul-13 12:32 
GeneralMy vote of 1 Pin
sujit_haldar7-Sep-12 4:54
membersujit_haldar7-Sep-12 4:54 
GeneralMy vote of 5 Pin
projectzombie8-Dec-11 16:47
memberprojectzombie8-Dec-11 16:47 
GeneralRe: My vote of 5 Pin
Chen Su8-Dec-11 17:05
memberChen Su8-Dec-11 17:05 
QuestionCould have been more simple... Pin
vantomas12-Aug-11 4:05
membervantomas12-Aug-11 4:05 
GeneralDelete Downloaded File Pin
Robairbutler10-Jun-10 22:58
memberRobairbutler10-Jun-10 22:58 
GeneralRe: Delete Downloaded File Pin
Chen Su11-Jun-10 14:46
memberChen Su11-Jun-10 14:46 
GeneralRe: Delete Downloaded File Pin
Robairbutler14-Jun-10 1:42
memberRobairbutler14-Jun-10 1:42 
QuestionPause / Resume Pin
khoanguyen11-Dec-09 18:02
memberkhoanguyen11-Dec-09 18:02 
QuestionChange download directory Pin
awelch23-Oct-09 5:46
memberawelch23-Oct-09 5:46 
AnswerRe: Change download directory Pin
Chen Su23-Oct-09 11:46
memberChen Su23-Oct-09 11:46 
QuestionHow to block IE generate temporary files? Pin
Xuefei.Wu13-Apr-09 17:09
memberXuefei.Wu13-Apr-09 17:09 
GeneralGood code, not very good article:) Pin
zengkun10012-Jun-08 1:04
memberzengkun10012-Jun-08 1:04 
Generalpause and resume Pin
monsieur_jj7-May-08 20:14
membermonsieur_jj7-May-08 20:14 
AnswerRe: pause and resume Pin
Chen Su8-May-08 9:14
memberChen Su8-May-08 9:14 
QuestionURLDownloadToCacheFile in Asynchronous mode Pin
call4e_friend21-May-07 21:56
membercall4e_friend21-May-07 21:56 
AnswerRe: URLDownloadToCacheFile in Asynchronous mode Pin
Chen Su22-May-07 19:15
memberChen Su22-May-07 19:15 
QuestionFile convertion Pin
kalaikumar.t10-Jul-06 23:53
memberkalaikumar.t10-Jul-06 23:53 
GeneralCommandline with Url Pin
RhettCawood10-Jul-06 19:50
memberRhettCawood10-Jul-06 19:50 
GeneralRe: Commandline with Url Pin
Chen Su13-Jul-06 13:56
memberChen Su13-Jul-06 13:56 
GeneralRe: Commandline with Url Pin
RhettCawood14-Jul-06 4:18
memberRhettCawood14-Jul-06 4:18 
GeneralVB Version of this downloader Pin
RhettCawood10-Jul-06 14:09
memberRhettCawood10-Jul-06 14:09 
GeneralRe: VB Version of this downloader Pin
Chen Su10-Jul-06 16:32
memberChen Su10-Jul-06 16:32 
Questionit do not work on my computer Pin
r0dmetal22-Mar-06 8:05
memberr0dmetal22-Mar-06 8:05 
GeneralAuthentication and URLDownloadToCacheFile Pin
JohnFx7-Mar-06 6:29
memberJohnFx7-Mar-06 6:29 
GeneralCheck the status Wether Connect to the internet or not Pin
Kutti Ra30-Sep-05 16:07
memberKutti Ra30-Sep-05 16:07 
QuestionUsing thru proxy ? Pin
darthmaul11-May-04 4:07
memberdarthmaul11-May-04 4:07 
AnswerRe: Using thru proxy ? Pin
Chen Su11-May-04 6:21
memberChen Su11-May-04 6:21 
GeneralRe: Using thru proxy ? Pin
darthmaul12-May-04 1:58
memberdarthmaul12-May-04 1:58 
GeneralRe: Using thru proxy ? Pin
Chen Su12-May-04 6:33
memberChen Su12-May-04 6:33 
GeneralRe: Using thru proxy ? Pin
Anonymous31-Aug-04 11:04
sussAnonymous31-Aug-04 11:04 
GeneralHandling errors of URL download ! Pin
Anonymous28-Apr-04 3:33
sussAnonymous28-Apr-04 3:33 
QuestionHow to implemet IBindStatusCallback Pin
Roland Mayné5-Feb-04 12:40
memberRoland Mayné5-Feb-04 12:40 
AnswerRe: How to implemet IBindStatusCallback Pin
Chen Su5-Feb-04 13:18
memberChen Su5-Feb-04 13:18 
GeneralRe: How to implemet IBindStatusCallback Pin
Roland Mayné10-Feb-04 4:11
sussRoland Mayné10-Feb-04 4:11 
GeneralRe: How to implemet IBindStatusCallback Pin
Chen Su10-Feb-04 8:24
memberChen Su10-Feb-04 8:24 
Generalnews.yahoo.com failed to download Pin
Anonymous27-Jan-03 22:27
sussAnonymous27-Jan-03 22:27 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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