Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
URL Monikers
Reference
Functions
 URLDownloadToFile Function

  Switch on low bandwidth view
URLDownloadToFile Function

Downloads bits from the Internet and saves them to a file.

Syntax

HRESULT URLDownloadToFile(      
    LPUNKNOWN pCaller,     LPCTSTR szURL,     LPCTSTR szFileName,     DWORD dwReserved,     LPBINDSTATUSCALLBACK lpfnCB );

Parameters

pCaller
A pointer to the controlling IUnknown interface of the calling Microsoft ActiveX component, if the caller is an ActiveX component. If the calling application is not an ActiveX component, this value can be set to NULL. Otherwise, the caller is a Component Object Model (COM) object that is contained in another component, such as an ActiveX control in the context of an HTML page. This parameter represents the outermost IUnknown of the calling component. The function attempts the download in the context of the ActiveX client framework, and allows the caller container to receive callbacks on the progress of the download.
szURL
A pointer to a string value that contains the URL to download. Cannot be set to NULL. If the URL is invalid, INET_E_DOWNLOAD_FAILURE is returned.
szFileName
A pointer to a string value containing the name or full path of the file to create for the download. If szFileName includes a path, the target directory must already exist.
dwReserved
Reserved. Must be set to 0.
lpfnCB
A pointer to the IBindStatusCallback interface of the caller. By using OnProgress, a caller can receive download status. URLDownloadToFile calls the OnProgress and OnDataAvailable methods as data is received. The download operation can be canceled by returning E_ABORT from any callback. This parameter can be set to NULL if status is not required.

Return Value

Returns one of the following values.

S_OKThe download started successfully.
E_OUTOFMEMORYThe buffer length is invalid, or there is insufficient memory to complete the operation.
INET_E_DOWNLOAD_FAILUREThe specified resource or callback interface was invalid.

Remarks

URLDownloadToFile binds to a host that supports IBindHost to perform the download. To do this, it first queries the controlling IUnknown passed as pCaller for IServiceProvider, then calls IServiceProvider::QueryService with SID_SBindHost. If pCaller does not support IServiceProvider, IOleObject or IObjectWithSite is used to query the object's host container. If no IBindHost interface is supported, or pCaller is NULL, URLDownloadToFile creates its own bind context to intercept download notifications.

URLDownloadToFile returns S_OK even if the file cannot be created and the download is canceled. If the szFileName parameter contains a file path, ensure that the destination directory exists before calling URLDownloadToFile. For best control over the download and its progress, an IBindStatusCallback interface is recommended.

 New for Windows Internet Explorer 8 Internet Explorer 8. URLDownloadToFile does not support IBindStatusCallbackEx and cannot be used to download files over 4 gigabytes (GB) in size. Refer instead to IBindStatusCallbackEx::GetBindInfoEx for a code example.

Function Information

Stock Implementationurlmon.dll
Custom ImplementationNo
HeaderUrlmon.h
Import libraryUrlmon.lib
Minimum availabilityInternet Explorer 3.0
Minimum operating systems Windows NT 4.0, Windows 95
UnicodeImplemented as ANSI and Unicode versions.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Should double check after getting S_OK      EricLaw-MSFT ... Thomas Lee   |   Edit   |   Show History
Note that this API returns S_OK if the file download succeeds but the file creation fails (e.g. because you specified a non-existent folder, drive, etc). Hence, you should ensure the file exists before attempting to operate on it.
Vista has additional return codes.      Walter Johnson ... Noelle Mallory - MSFT   |   Edit   |   Show History
Not entirely sure of all of the return codes, but the three listed with this article do not cover all possible return codes.
This was not helpful at all.      lcxdx58pl9 ... Thomas Lee   |   Edit   |   Show History
How about a sample program showing the use of this function?
Works with local IE cache.      Thomas Hamke ... Thomas Lee   |   Edit   |   Show History

If the file was changed on server, this method does not get the new file. The download will start from local IE cache. With 'DeleteUrlCacheEntry' it is possible to clear such cached data before getting the actual file.

DeleteUrlCacheEntry      kingces95   |   Edit   |   Show History
Wow, thanks a ton to Thomas Hamke for that note about 'DeleteUrlCacheEntry'. I might have figured out that for some reason I wasn't pulling the latest version down off the server but without that post I'm not sure I'd ever have figured out what API to call to clear the cache. DeleteUrlCacheEntry isn't even declared in the same urlmon -- it's way over in wininet! So thanks Thomas!
Tags What's this?: Add a tag
Flag as ContentBug
Seems to hang when used under Vista and network connection is lost      BarnyJuno   |   Edit   |   Show History
I use this function in a program that has to run under both Vista and XP, and I have to know when a network connection is lost during a file download. Under XP, everything is fine: disconnect the network, URLDownloadToFile exits immediately and I can begin to pick up the pieces. Under Vista, the function just hangs.
I've got a callback defined, and I thought that something would get reported through there, but no.

Anybody know what's going on?


Tags What's this?: Add a tag
Flag as ContentBug
Example for FreeBasic      KristopherWindsor ... Thomas Lee   |   Edit   |   Show History

I wonder if URLDownloadToFile and URLDownloadToFileA are the same thing?

Function utility_downloadfile (Byref url As String, Byref target As String) As Integer
Dim As Any Ptr library
Dim URLDownloadToFile As Function (Byval pCaller As Any Ptr, Byval szURL As String, _
Byval szFileName As String, Byval dwResv As Uinteger, Byval lpfnCB As Any Ptr) As Integer

library = Dylibload("urlmon")
If library = 0 Then Return false

URLDownloadToFile = Dylibsymbol(library, "URLDownloadToFileA")
If URLDownloadToFile = 0 Then Dylibfree(library): Return false

If URLDownloadToFile(0, url, target, 0, 0) Then Dylibfree(library): Return false

Dylibfree(library)
Return true
End Function

[tfl - 30 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio  : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public     : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
Return E_ABORT if canceled      Allen N.L. Lai ... Thomas Lee   |   Edit   |   Show History
This function returns E_ABORT when operation be canceled by returning E_ABORT from any callback.
If tearget file is in chinese then download fails?      Raghuram   |   Edit   |   Show History
I am dowloading the word file from the server and try to store in the local machine.So the local file name is in english then download success.But the local file name in chinese then it fails? is this accept chinese characters?
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker