Click here to Skip to main content
11,490,408 members (55,888 online)
Click here to Skip to main content

Using Internet Explorer to download files for you

, 27 Dec 1999 497.1K 8.8K 137
Rate this:
Please Sign up or sign in to vote.
This article presents two methods of using the URLDownloadToFile function in IE3+ to download files.
  • Download demo project - 23 Kb

    Sample Image - URLDownload.gif

    <!-- Article Starts -->

    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

    Share

    About the Author

    Michael Dunn
    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

     
    QuestionCan we start this with an task scheduler Pin
    saopramod24-Sep-13 11:13
    membersaopramod24-Sep-13 11:13 
    GeneralMy vote of 5 Pin
    Rob Caldecott13-Mar-11 11:12
    memberRob Caldecott13-Mar-11 11:12 
    GeneralMy vote of 5 Pin
    TClarke17-Feb-11 23:38
    memberTClarke17-Feb-11 23:38 
    QuestionWhy URLDownloadToFile always returns E_ABORT? Pin
    buaa_dep69-Dec-09 0:36
    memberbuaa_dep69-Dec-09 0:36 
    AnswerRe: Why URLDownloadToFile always returns E_ABORT? Pin
    buaa_dep69-Dec-09 15:57
    memberbuaa_dep69-Dec-09 15:57 
    Generalabout the project Pin
    munnangi17-Dec-08 21:26
    membermunnangi17-Dec-08 21:26 
    GeneralSave recordset to ADO file Pin
    Errol Obrique22-Sep-08 22:08
    memberErrol Obrique22-Sep-08 22:08 
    Hi all!

    If anyone knows the counterpart syntax of vb6 save method(ex: rs.save filename, adPersistADTG) in VB.Net. Please provide me the exact syntax. I just need an ADO File database(ex: filename.ado).. Many Thanks!!Smile | :)
    Questionability to continue download? Pin
    monsieur_jj24-Apr-08 21:25
    membermonsieur_jj24-Apr-08 21:25 
    GeneralRetrieving HTTP response code Pin
    Dave Shiel25-Oct-07 23:15
    memberDave Shiel25-Oct-07 23:15 
    GeneralRe: Retrieving HTTP response code Pin
    Michael Dunn6-Jan-08 9:25
    mvpMichael Dunn6-Jan-08 9:25 
    GeneralRe: Retrieving HTTP response code Pin
    Dave Shiel6-Jan-08 23:16
    memberDave Shiel6-Jan-08 23:16 
    GeneralRe: Retrieving HTTP response code Pin
    Michael Dunn7-Jan-08 4:59
    mvpMichael Dunn7-Jan-08 4:59 
    GeneralRe: Retrieving HTTP response code Pin
    DLChambers13-Mar-08 16:47
    memberDLChambers13-Mar-08 16:47 
    GeneralRe: Retrieving HTTP response code Pin
    phCourage6-Oct-09 0:10
    memberphCourage6-Oct-09 0:10 
    Generalerror = 800401E4 invalid syntax Pin
    awah1-Jul-07 7:20
    memberawah1-Jul-07 7:20 
    GeneralRe: error = 800401E4 invalid syntax Pin
    DLChambers13-Mar-08 16:18
    memberDLChambers13-Mar-08 16:18 
    AnswerRe: error = 800401E4 invalid syntax [modified] Pin
    Eric35806554-Aug-08 19:01
    memberEric35806554-Aug-08 19:01 
    GeneralRe: error = 800401E4 invalid syntax Pin
    DaleKing30-Sep-09 12:31
    memberDaleKing30-Sep-09 12:31 
    GeneralNice one Pin
    HakunaMatada15-Jun-07 20:06
    memberHakunaMatada15-Jun-07 20:06 
    GeneralRe: Nice one Pin
    Michael Dunn23-Jun-07 9:42
    mvpMichael Dunn23-Jun-07 9:42 
    GeneralRe: Nice one Pin
    HakunaMatada7-Sep-10 3:55
    memberHakunaMatada7-Sep-10 3:55 
    GeneralThanks a lot !!! Pin
    Harviz Harrison7-May-07 9:28
    memberHarviz Harrison7-May-07 9:28 
    QuestionHai [modified] Pin
    Arun.Immanuel5-May-07 20:59
    memberArun.Immanuel5-May-07 20:59 
    AnswerRe: Hai Pin
    Michael Dunn5-May-07 22:37
    mvpMichael Dunn5-May-07 22:37 
    QuestionSockets programming possible inside Internet Explorer Pin
    Member #5007105-Mar-07 7:05
    memberMember #5007105-Mar-07 7:05 
    QuestionHelp... Pin
    asfur22-Dec-06 6:22
    memberasfur22-Dec-06 6:22 
    QuestionIs there any way of getting title of not direct link? (php,asp, ets...) Pin
    vfilll9-Dec-06 10:53
    membervfilll9-Dec-06 10:53 
    AnswerRe: Is there any way of getting title of not direct link? (php,asp, ets...) Pin
    Michael Dunn9-Dec-06 16:14
    sitebuilderMichael Dunn9-Dec-06 16:14 
    GeneralRe: Is there any way of getting title of not direct link? (php,asp, ets...) Pin
    vfilll9-Dec-06 23:23
    membervfilll9-Dec-06 23:23 
    AnswerRe: Is there any way of getting title of not direct link? (php,asp, ets...) Pin
    Misutka22-Aug-08 5:09
    memberMisutka22-Aug-08 5:09 
    GeneralActiveX installer Pin
    Amit220320-Nov-06 3:29
    memberAmit220320-Nov-06 3:29 
    GeneralRe: ActiveX installer Pin
    Michael Dunn20-Nov-06 8:51
    sitebuilderMichael Dunn20-Nov-06 8:51 
    GeneralProblem when calling a Dialog to update the progress Pin
    sdancer7525-Jul-06 0:17
    membersdancer7525-Jul-06 0:17 
    GeneralAuthentication Pin
    JohnFx21-Mar-06 10:45
    memberJohnFx21-Mar-06 10:45 
    GeneralRe: Authentication Pin
    Michael Dunn22-Mar-06 11:52
    sitebuilderMichael Dunn22-Mar-06 11:52 
    GeneralRe: Authentication Pin
    JohnFx22-Mar-06 12:25
    memberJohnFx22-Mar-06 12:25 
    GeneralThanks a lot!! Pin
    Marchenko Ruslan6-Mar-06 6:24
    memberMarchenko Ruslan6-Mar-06 6:24 
    Generalyou are the man Pin
    Ben Bryant18-Oct-05 8:38
    memberBen Bryant18-Oct-05 8:38 
    GeneralRe: you are the man Pin
    Michael Dunn21-Oct-05 10:52
    sitebuilderMichael Dunn21-Oct-05 10:52 
    Generalnot using cache Pin
    JabraJabra18-Sep-05 21:29
    memberJabraJabra18-Sep-05 21:29 
    GeneralRe: not using cache Pin
    Michael Dunn21-Oct-05 10:48
    sitebuilderMichael Dunn21-Oct-05 10:48 
    GeneralError return! Pin
    Korivo28-Jul-05 6:39
    memberKorivo28-Jul-05 6:39 
    GeneralRe: Error return! Pin
    Michael Dunn29-Jul-05 10:52
    sitebuilderMichael Dunn29-Jul-05 10:52 
    GeneralRe: Error return! Pin
    Korivo29-Jul-05 12:34
    memberKorivo29-Jul-05 12:34 
    Generalhelp for this Pin
    tasleem14310-Jul-05 0:47
    membertasleem14310-Jul-05 0:47 
    QuestionThe ATL Way? Pin
    NotoriousBIG_PJ12-Jun-05 18:55
    memberNotoriousBIG_PJ12-Jun-05 18:55 
    AnswerRe: The ATL Way? Pin
    Michael Dunn13-Jun-05 11:47
    sitebuilderMichael Dunn13-Jun-05 11:47 
    General0x800401e4 Invalid syntax Pin
    djrisc29-Oct-04 23:41
    memberdjrisc29-Oct-04 23:41 
    GeneralRe: 0x800401e4 Invalid syntax Pin
    Michael Dunn30-Oct-04 6:32
    sitebuilderMichael Dunn30-Oct-04 6:32 
    GeneralPause and Resum !!!!!! Pin
    dongcm8212-Jul-04 19:25
    memberdongcm8212-Jul-04 19:25 

    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.150520.1 | Last Updated 28 Dec 1999
    Article Copyright 1999 by Michael Dunn
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid