Click here to Skip to main content
15,885,203 members
Articles / Web Development / HTML
Article

Using Internet Explorer to download files for you

Rate me:
Please Sign up or sign in to vote.
4.93/5 (39 votes)
27 Dec 1999 778.2K   10K   138   131
This article presents two methods of using the URLDownloadToFile function in IE3+ to download files.
  • Download demo project - 23 Kb

    Sample Image - URLDownload.gif

    Downloading with URLDownloadToFile()

    A while ago, Chris Maunder posted a suggestion to one of the boards asking how to download files, with the ability to abort the download after a timeout period. One solution to doing this is a function provided by IE 3 and later called URLDownloadToFile(). This function is suprisingly simple - just give it a URL, a filename, and an object to handle progress callbacks, and IE handles the rest!

    The function can be used by any application rather easily. IE displays no UI during the download, so the caller can provide any desired progress indicators. IE informs the caller of the download progress, so showing progress UI is also pretty easy.

    The prototype for URLDownloadToFile() is:

    HRESULT URLDownloadToFile(
        LPUNKNOWN pCaller,
        LPCSTR szURL,
        LPCSTR szFileName,
        DWORD dwReserved,
        LPBINDSTATUSCALLBACK lpfnCB);
    pCaller
    is used only if the caller is an ActiveX object. If the calling app is not an ActiveX object, this parameter can be NULL.
    szURL
    is the absolute URL of the file to download.
    szFileName
    contains the fully-qualified name of the file to be created.
    dwReserved
    must be zero.
    lpfnCB
    is a pointer to an IBindStatusCallback interface, which IE uses to inform you of the download progress.

    The steps involved in using URLDownloadToFile() are:

    • Obtain the URL for the file you want to download.
    • Construct the fully-qualified path for the file to be created.
    • Create an IBindStatusCallback-derived class and write an OnProgress() function for it.
    • Instantiate an object of that class.
    • Call URLDownloadToFile(). The call is synchronous, so you will probably want to call the function from a worker thread.
    • In your OnProgress() function, provide any progress indicators or other UI that you want. The return from OnProgress() tells IE whether to continue the download, or abort it.

    Using IBindStatusCallback

    IBindStatusCallback has eight methods, but for the purposes of downloading, the only function you need to be concerned with is OnProgress(). The others can just return E_NOTIMPL. The prototype for OnProgress() is:

    HRESULT OnProgress(
        ULONG  ulProgress,
        ULONG  ulProgressMax,
        ULONG  ulStatusCode,
        LPCWSTR  szStatusText );
    ulProgress
    is the number of bytes downloaded so far.
    ulProgressMax
    is the size of the file, or zero if the size is not known. Note that technically, this value may change between calls to OnProgress(), so you should not store the value in a static variable; you should check the value every time the function is called.
    ulStatusCode
    is a number indicating whether the download is in progress. The value can be BINDSTATUS_BEGINDOWNLOADCOMPONENTS, BINDSTATUS_INSTALLINGCOMPONENTS, or BINDSTATUS_ENDDOWNLOADCOMPONENTS.
    szStatusText
    is a string suitable for using in UI, if you want to take what IE gives you. This text is pretty bare-bones, and you'll probably want to use something more substantial for your progress UI. Note that technically, this parameter may be NULL, so be sure to check the value before using it.

    OnProgress() returns S_OK to tell IE to keep downloading, or E_ABORT to abort the download.

    The ATL Way

    If you have looked through the MSDN docs a bit, you may have seen that ATL has a CBindStatusCallback class. This class implements IBindStatusCallback for you. When using CBindStatusCallback, you provide ATL an object that has a couple of member functions that ATL will call back as the download proceeds. The object is given to ATL as the template parameter "T" in CBindStatusCallback<T>.

    Using this class involves more work, however, since you must provide a function that stores the incoming data. When you use URLDownloadToFile(), the data is saved for you automatically.

    The Sample Project

    The screen shot at the beginning of the article is from the sample project's app. You enter the URL of the file you want to download, and the full path of the target file on your system, and click Start. You can also enter a timeout in seconds, and the app will abort the download if that length of time passes before the donwload is complete.

    You can find the latest updates to this any my other articles at http://home.inreach.com/mdunn/code/

  • 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


    Written By
    Software Developer (Senior) VMware
    United States United States
    Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
    Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

    He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

    Mike was a VC MVP from 2005 to 2009.

    Comments and Discussions

     
    GeneralFirewall (NTLM Authentication) Pin
    serpico23-Jan-04 2:42
    serpico23-Jan-04 2:42 
    GeneralRe: Firewall (NTLM Authentication) Pin
    Michael Dunn15-Apr-04 6:45
    sitebuilderMichael Dunn15-Apr-04 6:45 
    QuestionCannot donwload an EXE file ? Pin
    Braulio Dez20-Jan-04 23:33
    Braulio Dez20-Jan-04 23:33 
    AnswerRe: Cannot donwload an EXE file ? Pin
    DanYELL15-Apr-04 6:22
    DanYELL15-Apr-04 6:22 
    GeneralRe: Cannot donwload an EXE file ? Pin
    Braulio Dez15-Apr-04 20:08
    Braulio Dez15-Apr-04 20:08 
    AnswerRe: Cannot donwload an EXE file ? Pin
    Michael Dunn15-Apr-04 6:50
    sitebuilderMichael Dunn15-Apr-04 6:50 
    GeneralRe: Cannot donwload an EXE file ? Pin
    Braulio Dez15-Apr-04 20:09
    Braulio Dez15-Apr-04 20:09 
    QuestionIE how to program ? Pin
    Tuan, Nguyen Hoang11-Jan-04 21:28
    Tuan, Nguyen Hoang11-Jan-04 21:28 
    QuestionURLDownloadToFile Return Error? Pin
    siewlien15-Dec-03 16:14
    siewlien15-Dec-03 16:14 
    GeneralVisual Basic Pin
    Anonymous1-Dec-03 3:53
    Anonymous1-Dec-03 3:53 
    GeneralRe: Visual Basic Pin
    ExtraLean17-Sep-04 7:21
    ExtraLean17-Sep-04 7:21 
    GeneralResume cache &amp; Err Pin
    Anonymous26-Oct-03 2:01
    Anonymous26-Oct-03 2:01 
    GeneralRe: Resume cache &amp; Err Pin
    Anonymous26-Oct-03 4:47
    Anonymous26-Oct-03 4:47 
    GeneralError Occur When Visit a Website which require authentication Pin
    Jason_C.S.H24-Jul-03 3:36
    Jason_C.S.H24-Jul-03 3:36 
    GeneralRe: Error Occur When Visit a Website which require authentication Pin
    Leithal Weapon22-Dec-04 9:29
    sussLeithal Weapon22-Dec-04 9:29 
    GeneralRe: Error Occur When Visit a Website which require authentication Pin
    Michael Dunn22-Dec-04 16:39
    sitebuilderMichael Dunn22-Dec-04 16:39 
    GeneralMichael: There is a big bug :) in your application Pin
    Devpro AB20-Jul-03 10:41
    Devpro AB20-Jul-03 10:41 
    QuestionHow can I get Abort in CBindStatusCallback?? Pin
    zzangada18-Jun-03 14:52
    zzangada18-Jun-03 14:52 
    QuestionWhy this method can not be used at some website? Pin
    8K994-Apr-03 5:00
    8K994-Apr-03 5:00 
    AnswerRe: Why this method can not be used at some website? Pin
    Michael Dunn5-Apr-03 5:47
    sitebuilderMichael Dunn5-Apr-03 5:47 
    GeneralFile Attributes Pin
    Anonymous12-Nov-02 9:37
    Anonymous12-Nov-02 9:37 
    GeneralRe: File Attributes Pin
    Michael Dunn16-Nov-02 17:13
    sitebuilderMichael Dunn16-Nov-02 17:13 
    GeneralRe: File Attributes Pin
    Snillet2k27-Jan-03 4:31
    Snillet2k27-Jan-03 4:31 
    QuestionHow can I determin the default filename to download. Pin
    itblue18-Oct-02 2:10
    itblue18-Oct-02 2:10 
    QuestionDownload whole website? Pin
    Softwar soldier24-Aug-02 21:42
    Softwar soldier24-Aug-02 21:42 

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

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