Click here to Skip to main content
6,306,412 members and growing! (18,804 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Intermediate

Download a File Using URLDownloadToCacheFile

By Chen Su

A sample to demonstrate how to use URLDownloadToCacheFile and IBindStatusCallback.
VC6, VC7Win2K, WinXP, MFC, Dev
Posted:25 Oct 2002
Views:85,119
Bookmarked:43 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
20 votes for this article.
Popularity: 5.24 Rating: 4.03 out of 5
1 vote, 6.3%
1

2

3
7 votes, 43.8%
4
8 votes, 50.0%
5

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


Member

Occupation: Software Developer
Location: Canada Canada

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 26 (Total in Forum: 26) (Refresh)FirstPrevNext
GeneralHow to block IE generate temporary files? PinmemberXuefei.Wu18:09 13 Apr '09  
GeneralGood code, not very good article:) Pinmemberzengkun1002:04 12 Jun '08  
Generalpause and resume Pinmembermonsieur_jj21:14 7 May '08  
AnswerRe: pause and resume PinmemberChen Su10:14 8 May '08  
QuestionURLDownloadToCacheFile in Asynchronous mode Pinmembercall4e_friend22:56 21 May '07  
AnswerRe: URLDownloadToCacheFile in Asynchronous mode PinmemberChen Su20:15 22 May '07  
QuestionFile convertion Pinmemberkalaikumar.t0:53 11 Jul '06  
GeneralCommandline with Url PinmemberRhettCawood20:50 10 Jul '06  
GeneralRe: Commandline with Url PinmemberChen Su14:56 13 Jul '06  
GeneralRe: Commandline with Url PinmemberRhettCawood5:18 14 Jul '06  
GeneralVB Version of this downloader PinmemberRhettCawood15:09 10 Jul '06  
GeneralRe: VB Version of this downloader PinmemberChen Su17:32 10 Jul '06  
Questionit do not work on my computer Pinmemberr0dmetal9:05 22 Mar '06  
GeneralAuthentication and URLDownloadToCacheFile PinmemberJohnFx7:29 7 Mar '06  
GeneralCheck the status Wether Connect to the internet or not PinmemberKutti Ra17:07 30 Sep '05  
GeneralUsing thru proxy ? Pinmemberdarthmaul5:07 11 May '04  
GeneralRe: Using thru proxy ? PinmemberChen Su7:21 11 May '04  
GeneralRe: Using thru proxy ? Pinmemberdarthmaul2:58 12 May '04  
GeneralRe: Using thru proxy ? PinmemberChen Su7:33 12 May '04  
GeneralRe: Using thru proxy ? PinsussAnonymous12:04 31 Aug '04  
GeneralHandling errors of URL download ! PinsussAnonymous4:33 28 Apr '04  
GeneralHow to implemet IBindStatusCallback PinmemberRoland Mayné13:40 5 Feb '04  
GeneralRe: How to implemet IBindStatusCallback PinmemberChen Su14:18 5 Feb '04  
GeneralRe: How to implemet IBindStatusCallback PinsussRoland Mayné5:11 10 Feb '04  
GeneralRe: How to implemet IBindStatusCallback PinmemberChen Su9:24 10 Feb '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Oct 2002
Editor: Chris Maunder
Copyright 2002 by Chen Su
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project