Two ways to download files from the network

Source: Internet
Author: User

The COM functions used by both methods are derived from the Urlmon, remember to refer to them first.

First, Urldownloadtofile

To download files directly to disk, Urldownloadtofile declares as follows:

function Urldownloadtofile (
Caller:iunknown;
Url:pwidechar; {Path to download}
Filename:pwidechar; {The path saved to}
Reserved:dword;
Statuscb:ibindstatuscallback
): HResult; stdcall;

Urldownloadtofilea (Caller:iunknown; Url:pansichar; Filename:pansichar; Reserved:dword; Statuscb:ibindstatuscallback): HResult; stdcall; function Urldownloadtofilew (Caller:iunknown; Url:pwidechar; Filename:pwidechar; Reserved:dword; Statuscb:ibindstatuscallback): HResult; stdcall;

Invocation Example:
If Urldownloadtofile (nil, ' http://www.xxxx.com/xxx.xx ', ' c:/1.xx ', 0, nil) = 0 Then
MessageBox (handle, ' download successful ', ' ', 0)

Else

MessageBox (handle, ' download failed ', ' ', 0);


Urldownloadtofile before downloading the file in the local cache to find the file, if the cache will not go to the internet to catch the latest files, you want to always get a new file when you can add a random number after the URL, or with deleteurlcacheentry (URL) Clean up the cache before downloading

Second, Urlopenstream

Download to IStream Stream, declared as:

function Urlopenstream (
P1:iunknown;

P2:pwidechar; {Path to download}

P3:dword;

P4:ibindstatuscallback {interface, which obtains data through it after download}

): HResult; stdcall;

function Urlopenstreama (P1:iunknown; p2:pansichar; p3:dword; p4:ibindstatuscallback): HResult; stdcall;

function URLOPENSTREAMW (P1:iunknown; p2:pwidechar; p3:dword; p4:ibindstatuscallback): HResult; stdcall;

Implementation of the IBindStatusCallback interface

Tbindstatuscallback =class(Tinterfacelist, IBindStatusCallback) Public    functionOnstartbinding (Dwreserved:dword; pib:ibinding): HResult;stdcall;functionGetPriority ( outnpriority): HResult;stdcall;functionOnlowresource (Reserved:dword): HResult;stdcall;functionOnProgress (ulprogress, ulProgressMax, Ulstatuscode:ulong; SZSTATUSTEXT:LPCWSTR): HResult;stdcall;functionOnstopbinding (Hresult:hresult; szerror:lpcwstr): HRESULT;stdcall;functionGetbindinfo ( outGrfbindf:dword;varBindinfo:tbindinfo): HResult;stdcall;functionOnDataAvailable (Grfbscf:dword; dwsize:dword; formatetc:pformatetc; Stgmed:pstgmedium): HResult;stdcall;functionOnobjectavailable (ConstIid:tguid; Punk:iunknown): HResult;stdcall;

End;

{Tbindstatuscallback}functionTbindstatuscallback.getbindinfo ( outGrfbindf:dword;varBindinfo:tbindinfo): HResult;beginResult: = S_OK;End;functionTbindstatuscallback.getpriority ( outnpriority): HResult;beginResult: = S_OK;End;functionTbindstatuscallback.ondataavailable (GRFBSCF, Dwsize:dword; FORMATETC:PFORMATETC; Stgmed:pstgmedium): HResult;varStream:istream; Ms:tmemorystream;begin  ifdwsize > 0 Then  beginStream: = IStream (stgmed.stm); MS: = tmemorystream.create;
Ms. SetSize (dwsize);
Stream.read (MS. Memory, dwsize,Nil);
Fetching data from MS ...
Ms. Free;
Result: = S_OK;End ElseResult: = E_abort;End;functionTbindstatuscallback.onlowresource (Reserved:dword): HResult;beginResult: = S_OK;End;functionTbindstatuscallback.onobjectavailable (ConstIid:tguid; Punk:iinterface): HResult;beginResult: = S_OK;End;functionTbindstatuscallback.onprogress (ulprogress, ulProgressMax, Ulstatuscode:ulong; szstatustext:lpcwstr): HResult;beginCan realize the detection progress, the implementation code writes Result: = S_OK;End;functionTbindstatuscallback.onstartbinding (Dwreserved:dword; pib:ibinding): HResult;beginResult: = S_OK;End;functionTbindstatuscallback.onstopbinding (Hresult:hresult; SZERROR:LPCWSTR): HResult;beginResult: = S_OK;End;
Calling code:
var
string;
Mybindstatuscallback:ibindstatuscallback;
begin  Mybindstatuscallback: = tbindstatuscallback.create;  if Succeeded (Urlopenstream (nil Then
Download successful! Else Download failed!; End;

http://blog.csdn.net/nhconch/article/details/5470777

Two ways to download files from the network

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.