Download a File Using URLDownloadToCacheFile

This article was contributed by chensu.

The User Interface

Environment: Visual C++ 6.0 with Service Pack 3, Windows 9x and Windows NT 4.0 with Service Pack 3

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.

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


Downloads

Download Source Code - 84KB (New updated file)




Comments

  • URLDownloadToCacheFile in Asynchronous mode

    Posted by call4e_friend on 05/22/2007 04:54am

    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

    Reply
  • https ?

    Posted by Legacy on 02/10/2004 12:00am

    Originally posted by: Greg S

    This is a fine code, but can it be modified to download files from an HTTPS source ?

    Reply
  • Try CExtDownload

    Posted by Legacy on 08/18/2003 12:00am

    Originally posted by: marcus gesing

    Source and demo:
    http://home.arcor.de/footwear-box/various/coding/ExtLib.zip

    Reply
  • Download progress in Visual C

    Posted by Legacy on 01/08/2003 12:00am

    Originally posted by: Ambrose

    Does anyone know how to check the progress of downloading a file using http in Visual C?

    Reply
  • it won't down load http://news.yahoo.com

    Posted by Legacy on 12/11/2002 12:00am

    Originally posted by: Donna


    it works for most of pages, but
    it won't down load http://news.yahoo.com.

    what's the problem and how to fix it?

    thanks for your attention.

    Donna.

    Reply
  • Store URL

    Posted by Legacy on 11/21/2002 12:00am

    Originally posted by: eddie

    Hi,
    
    

    may I know how to download the url without pressing the download button, meaning it will download once you key in the url and press enter instead.

    In addition does anyone know how to store the url into the memory cache of the computer system?

    Thank you for your attention.

    Regards
    eddie

    Reply
  • Download the whole website (http server)

    Posted by Legacy on 08/23/2002 12:00am

    Originally posted by: Felix

    I am wondering how to download all the website using MFC?

    Reply
  • URLDownloadToCacheFile looses extension

    Posted by Legacy on 08/22/2002 12:00am

    Originally posted by: Ralf Berger

    when retrieving files from some urls (like ../../../../x/file.abc) the
    extension of the downloaded file is switched from .ABC to .HTM.

    this happens only some times, other urls don't receive the wrong extension.

    any ideas?

    thanks
    - ralf

    Reply
  • Any one know how to do this in VB

    Posted by Legacy on 07/23/2002 12:00am

    Originally posted by: Mike

    Does any on on this planet know how to monitor the progress of a Download in Visual Basic 6....

    Reply
  • Relative Path Names

    Posted by Legacy on 03/13/2002 12:00am

    Originally posted by: Rob Bairos


    Hello. Ive experimented with this file
    on my own, and find it fails with relative
    path names. The URL parameter must be
    fully specified. This makes for some
    non-portable html coding.

    Have you had any experience with this
    problem?

    Thanks very much.

    Rob Bairos.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds