URLDownloadToFile

URLDownloadToFile

簡介,Delphi,C++,vb,

簡介

指從指定URL地址讀取內容並將讀取到的內容保存到特定的檔案里,下面列舉了幾種常見程式語言的實現方法。

Delphi

⒈uses UrlMon;
⒉//-------------------------------------------------------------------------------
functionDownloadFile(SourceFile, DestFile: string): Boolean;
⒋// 參數 下載檔案路徑, 保存檔案路徑
⒌// 返回 下載結果
⒍ begin
⒎ try
⒏ Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0;
⒐ except
⒑ Result := False;
⒒ end;
⒓ end;
var
⒉ url,destFile:string;
⒊url := 'http****filename.rar'
⒋destFile := 'E:\filename.rar'
ifDownloadFile(url, DestFile)then
⒍ begin
⒎ ShowMessage('保存成功!');
⒏ end
⒐ else
⒑ begin
⒒ ShowMessage('保存不成功!');
⒓ end;

C++

Syntax:
HRESULT URLDownloadToFile(     LPUNKNOWN pCaller,    LPCTSTR szURL,    LPCTSTR szFileName,    DWORD dwReserved,    LPBINDSTATUSCALLBACK lpfnCB);
Parameters:
pCallerPointer to the controlling IUnknown interface of the calling Microsoft ActiveX component (if the caller is an ActiveX component). //控制項的接口,如果不是控制項則為0.
szURL
Pointer to a string value containing the URL to be downloaded. Cannot be set to NULL
//要下載的url地址,不能為空.
szFileName
Pointer to a string value containing the name of the file to create for bits that come from the download.
//下載後保存的檔案名稱.
dwReserved
Reserved. Must be set to 0.
//保留欄位,必需為0
lpfnCB
Pointer to the caller's IBindStatusCallback interface. URLDownloadToFile calls this interface's IBindStatusCallback::OnProgress method on a connection activity, including the arrival of data. IBindStatusCallback::OnDataAvailable is never called.
//下載進度狀態回調
Return Value
Returns one of the following values.
S_OK : The download started successfully.
E_OUTOFMEMORY: The buffer length is invalid, or there is insufficient memory to complete the operation.
INET_E_DOWNLOAD_FAILURE:The specified resource or callback interface was invalid.
Example:
#include "stdafx.h"#include <iostream>#include <UrlMon.h>#pragma comment(lib, "urlmon.lib") using namespace std;int _tmain(int argc, _TCHAR* argv[]){    HRESULT hr = URLDownloadToFile(0,_T("***ui.the9.**/wshell/WShell.exe"),    _T ("D:\\WShell.exe"), 0,NULL);    if (hr== S_OK)    {        cout<< "ok"<< endl;    }    return 0;}

vb

該函式是VB的一個用來調用API進行網路下載的函式,過程如下:
後台下載
聲明:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
'//調用
然後在代碼里直接調用:
Call URLDownloadToFile(0, "網頁地址", "保存到本地檔案名稱和地址", 0, 0)
Call URLDownloadToFile(0, "*****.baidu**/", "c:\1.htm", 0, 0)
相當於另外儲存為
聲明:
Private Declare Function DoFileDownload Lib "shdocvw.dll"(ByVal lpszFile As String) As Long
'//調用
然後在代碼里直接調用:
Dim gourl As String
gourl = StrConv("網頁地址", vbUnicode)
Call DoFileDownload(gourl)

相關詞條

熱門詞條

聯絡我們