首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

文件下载,该如何解决

2012-02-11 
文件下载我现在使用的下载代码如下:C/C++ code//进度显示for(bar-Position0bar-Position bar-Maxba

文件下载
我现在使用的下载代码如下:

C/C++ code
//进度显示for(bar->Position=0;bar->Position< bar->Max;bar->StepIt()){    //下载文件    URLDownloadToFile(NULL,"http://localhost/Demo4/install.exe","download\\install.exe",0,NULL);    lab->Caption="下载成功";}

存在的问题:
1、如果url文件不可访问,会出错,该如何控制?
2、如何判断该文件是否下载成功(下载完成)?
3、进度条的用法是否存在问题?

在线等,谢谢。

[解决办法]
http://blog.csdn.net/aniven/archive/2007/06/03/1636760.aspx

OnProgress 实现的规范是: 
HRESULT OnProgress( 
ULONG ulProgress, 
ULONG ulProgressMax, 
ULONG ulStatusCode, 
LPCWSTR szStatusText
);

ulProgress
到目前为止已经下载的字节数。 
ulProgressMax
要下载的文件大小,0表示大小未知,需要注意的是,这个值仅在OnProgress调用期间变化,所以你不能把它保存到一个静态变量中,你应该在每次调用时检查这个值。
ulStatusCode
状态标志,这个值可以为下列一些值: 
BINDSTATUS_BEGINDOWNLOADCOMPONENTS
BINDSTATUS_INSTALLINGCOMPONENTS
BINDSTATUS_ENDDOWNLOADCOMPONENTS
szStatusText
图形界面中使用的字符串,由IE 提供,这个变量可能为NULL,在使用前应该检查这个变量。 

OnProgress()返回 S_OK 告诉IE要继续下载, E_ABORT则表示放弃下载。


[解决办法]
以下是一个完整例子,在Win7 + CB6环境下测试成功。

C/C++ code
#include <UrlMon.h>#pragma link "C:\\Program Files\\Borland\\CBuilder6\\\Lib\\Psdk\\urlmon.lib"class TCrnCallback : public IBindStatusCallback{private:    TProgressBar *m_pb;    DWORD m_cRef;    STDMETHODIMP QueryInterface(REFIID riid, void **ppv);    STDMETHODIMP_(ULONG) AddRef();    STDMETHODIMP_(ULONG) Release();    STDMETHODIMP GetBindInfo(DWORD *grfBINDF, BINDINFO *bindinfo);    STDMETHODIMP GetPriority(LONG *nPriority);    STDMETHODIMP OnDataAvailable(DWORD grfBSCF, DWORD dwSize,        FORMATETC *formatetc, STGMEDIUM *stgmed);    STDMETHODIMP OnLowResource(DWORD reserved);    STDMETHODIMP OnObjectAvailable(REFIID iid, IUnknown *punk);    STDMETHODIMP OnStartBinding(DWORD dwReserved, IBinding *pib);    STDMETHODIMP OnStopBinding(HRESULT hresult, LPCWSTR szError);    STDMETHODIMP OnProgress(ULONG ulProgress, ULONG ulProgressMax,        ULONG ulStatusCode, LPCWSTR szStatusText);public:    TCrnCallback(TProgressBar *pb): m_pb(pb) {m_cRef = 1;};};STDMETHODIMP TCrnCallback::QueryInterface(REFIID riid, void **ppv){    *ppv = NULL;    if (riid == IID_IUnknown || riid == IID_IBindStatusCallback)    {        *ppv = this;        AddRef();        return S_OK;    }    return E_NOINTERFACE;}STDMETHODIMP_(ULONG) TCrnCallback::AddRef(){    return m_cRef++;}STDMETHODIMP_(ULONG) TCrnCallback::Release(){    if(--m_cRef == 0)    {        delete this;        return 0;    }    return m_cRef;}STDMETHODIMP TCrnCallback::GetBindInfo(DWORD *grfBINDF, BINDINFO *bindinfo){    return E_NOTIMPL;}STDMETHODIMP TCrnCallback::GetPriority(LONG *nPriority){    return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnDataAvailable(DWORD grfBSCF, DWORD dwSize,     FORMATETC *formatetc, STGMEDIUM *stgmed){     return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnLowResource(DWORD reserved){    return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnObjectAvailable(REFIID iid, IUnknown *punk){    return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnStartBinding(DWORD dwReserved, IBinding *pib){    return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnStopBinding(HRESULT hresult, LPCWSTR szError){    return E_NOTIMPL;}STDMETHODIMP TCrnCallback::OnProgress(ULONG ulProgress, ULONG ulProgressMax,    ULONG ulStatusCode, LPCWSTR szStatusText){     m_pb->Max = ulProgressMax;    m_pb->Position = ulProgress;    return S_OK;}void __fastcall TForm1::Button1Click(TObject *Sender){    TCrnCallback *pCallback = new TCrnCallback(ProgressBar1);    try    {        URLDownloadToFile(NULL, TEXT("http://www.ccrun.com/temp/htmlinres.zip"), TEXT("C:\\123.zip"), 0, pCallback);    }    __finally    {        delete pCallback;    }    ShowMessage("完毕");} 

热点排行