为什么UrlDownloadToFile :: OnProgress总是返回具有相同值的ulProgress和ulProgressMax [英] Why UrlDownloadToFile::OnProgress always return ulProgress and ulProgressMax with the same value

查看:57
本文介绍了为什么UrlDownloadToFile :: OnProgress总是返回具有相同值的ulProgress和ulProgressMax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的delphi代码:

This is my delphi code:

TDownloadCallback = class(TInterfacedObject, IBindStatusCallback)
private
    fOwner: TDownload;
    fUrl: string;
public
    constructor Create(owner: TDownload; url: string);

    function OnStartBinding(dwReserved: DWORD; pib: IBinding): HResult; stdcall;
    function GetPriority(out nPriority): HResult; stdcall;
    function OnLowResource(reserved: DWORD): HResult; stdcall;
    function OnProgress(ulProgress, ulProgressMax, ulStatusCode: ULONG; szStatusText: LPCWSTR): HResult; stdcall;
    function OnStopBinding(hresult: HResult; szError: LPCWSTR): HResult; stdcall;
    function GetBindInfo(out grfBINDF: DWORD; var bindinfo: TBindInfo): HResult; stdcall;
    function OnDataAvailable(grfBSCF: DWORD; dwSize: DWORD; formatetc: PFormatEtc; stgmed: PStgMedium): HResult; stdcall;
    function OnObjectAvailable(const iid: TGUID; punk: IUnknown): HResult; stdcall;
end;

TDownloadStatus =
(
dsNULL                , dsFindingresource         , dsConnecting              , dsRedirecting,
dsBegindownloaddata   , dsDownloadingdata         , dsEnddownloaddata         , dsBegindownloadcomponents,
dsInstallingcomponents, dsEnddownloadcomponents   , dsUsingcachedcopy         , dsSendingrequest,
dsClassidavailable    , dsMimetypeavailable       , dsCachefilenameavailable  , dsBeginsyncoperation,
dsEndsyncoperation    , dsBeginuploaddata         , dsUploadingdata           , dsEnduploadingdata,
dsProtocolclassid     , dsEncoding                , dsVerfiedmimetypeavailable, dsClassinstalllocation,
dsDecoding            , dsLoadingmimehandler      , dsContentdispositionattach, dsFilterreportmimetype,
dsClsidcaninstantiate , dsIunknownavailable       , dsDirectbind              , dsRawmimetype,
dsProxydetecting      , dsAcceptranges
);

const
  WinNetLib = 'Wininet.DLL';

function DeleteUrlCacheEntry(pszUrl: PChar): BOOL; stdcall; external WinNetLib name 'DeleteUrlCacheEntry';

{ TDownloadCallback }

constructor TDownloadCallback.Create(owner: TDownload; url: string);
begin
  fOwner := owner;
  fUrl := url;
end;

function TDownloadCallback.GetBindInfo(out grfBINDF: DWORD;
  var bindinfo: TBindInfo): HResult;
begin
  DeleteUrlCacheEntry(PChar(fUrl));
  Result := S_OK;
end;

function TDownloadCallback.GetPriority(out nPriority): HResult;
begin
  Result := S_OK;
end;

function TDownloadCallback.OnDataAvailable(grfBSCF, dwSize: DWORD;
  formatetc: PFormatEtc; stgmed: PStgMedium): HResult;
begin
  Result := S_OK;
end;

function TDownloadCallback.OnLowResource(reserved: DWORD): HResult;
begin
  Result := S_OK;
end;

function TDownloadCallback.OnObjectAvailable(const iid: TGUID;
  punk: IInterface): HResult;
begin
  Result := S_OK;
end;

function TDownloadCallback.OnProgress(ulProgress, ulProgressMax,
  ulStatusCode: ULONG; szStatusText: LPCWSTR): HResult;
var
  status: TDownloadStatus;
begin
  if fOwner.fProgress <> nil then
  begin
    status := TDownloadStatus(ulStatusCode);
    case (status) of
    dsBegindownloaddata:
      begin
        fOwner.fProgress.Max := ulProgressMax;
        DeleteUrlCacheEntry(PChar(szStatusText));
      end;
    dsDownloadingdata:
      begin
        // progressbar here!
      end;
    end;
  end;

  Form1.Memo1.Lines.Add(Format('ulProgress: %d, ulProgressMax: %d, ulStatusCode: %d (%s), szStatusText: %s', [
    ulProgress, ulProgressMax, ulStatusCode, GetEnumName(TypeInfo(TDownloadStatus), Integer(status)), szStatusText
  ]));

  Result := S_OK;
  Application.ProcessMessages;
end;

function TDownloadCallback.OnStartBinding(dwReserved: DWORD;
  pib: IBinding): HResult;
begin
  Result := S_OK;
end;

function TDownloadCallback.OnStopBinding(hresult: HResult;
  szError: LPCWSTR): HResult;
begin
  Result := S_OK;
end;

procedure download(url, filename: string);
var
  intf: IBindStatusCallback;
begin
  CoInitialize(nil);
  try
    intf := TDownloadCallback.Create(self, url);
    try
      URLDownloadToFile(nil, PChar(url), Pchar(filename), 0, intf);
    finally
      intf := nil;
    end;
  finally
    CoUninitialize;
  end;
end;

当我呼叫 download('htp://....','filename.exe')时,我的 memo 打印:

When I call download('htp://....', 'filename.exe') my memo prints:

ulProgress: 0, ulProgressMax: 0, ulStatusCode: 32 (dsProxydetecting), szStatusText: 
ulProgress: 0, ulProgressMax: 0, ulStatusCode: 1 (dsFindingresource), szStatusText: www.jjw.com.br
ulProgress: 0, ulProgressMax: 0, ulStatusCode: 2 (dsConnecting), szStatusText: 177.200.200.199
ulProgress: 0, ulProgressMax: 0, ulStatusCode: 11 (dsSendingrequest), szStatusText: 
ulProgress: 0, ulProgressMax: 0, ulStatusCode: 64 (@
ulProgress: 0, ulProgressMax: 0, ulStatusCode: 3 (dsRedirecting), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 0, ulProgressMax: 0, ulStatusCode: 32 (dsProxydetecting), szStatusText: 
ulProgress: 0, ulProgressMax: 0, ulStatusCode: 1 (dsFindingresource), szStatusText: get.anydesk.com
ulProgress: 0, ulProgressMax: 0, ulStatusCode: 2 (dsConnecting), szStatusText: 188.40.104.135
ulProgress: 0, ulProgressMax: 0, ulStatusCode: 11 (dsSendingrequest), szStatusText: 
ulProgress: 0, ulProgressMax: 0, ulStatusCode: 26 (dsContentdispositionattach), szStatusText: C:\Users\JJW\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\YQIDXOIQ\jjwsuporteremotoFAYWSU4M.exe
ulProgress: 0, ulProgressMax: 0, ulStatusCode: 13 (dsMimetypeavailable), szStatusText: application/x-msdownload
ulProgress: 32398, ulProgressMax: 32398, ulStatusCode: 4 (dsBegindownloaddata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 32398, ulProgressMax: 32398, ulStatusCode: 14 (dsCachefilenameavailable), szStatusText: C:\Users\JJW\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\YQIDXOIQ\jjwsuporteremotoFAYWSU4M.exe
ulProgress: 48774, ulProgressMax: 48774, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 65158, ulProgressMax: 65158, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 81534, ulProgressMax: 81534, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 97918, ulProgressMax: 97918, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 114294, ulProgressMax: 114294, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 130678, ulProgressMax: 130678, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 147054, ulProgressMax: 147054, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 163438, ulProgressMax: 163438, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 179814, ulProgressMax: 179814, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 196198, ulProgressMax: 196198, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 212574, ulProgressMax: 212574, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 278094, ulProgressMax: 278094, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 294478, ulProgressMax: 294478, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 327238, ulProgressMax: 327238, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 343614, ulProgressMax: 343614, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 392758, ulProgressMax: 392758, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 474654, ulProgressMax: 474654, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 540174, ulProgressMax: 540174, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 572934, ulProgressMax: 572934, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 589318, ulProgressMax: 589318, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 1326414, ulProgressMax: 1326414, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 1342798, ulProgressMax: 1342798, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 1424694, ulProgressMax: 1424694, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 1441078, ulProgressMax: 1441078, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 1473838, ulProgressMax: 1473838, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 1490214, ulProgressMax: 1490214, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 1703158, ulProgressMax: 1703158, ulStatusCode: 5 (dsDownloadingdata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe
ulProgress: 2127952, ulProgressMax: 2127952, ulStatusCode: 6 (dsEnddownloaddata), szStatusText: https://get.anydesk.com/psTS1FIS/jjwsuporteremoto.exe

请注意, ulProgress ulPorgressMax 始终返回相同的值,并且它会改变.

Note that the ulProgress and ulPorgressMax always return the same value and it changes.

如何使用 UrlDownloadToFile 根据文件大小(Content-Length标头)创建真实的进度栏?

How can I create a real progressbar based on the file size (Content-Length header) using the UrlDownloadToFile ?

推荐答案

某些Web服务器不提供内容长度标头,因此,文件大小的最佳猜测是到目前为止已收到多少.

Some web servers do not provide the content length header so the best guess of file size is how much has been received so far.

这篇关于为什么UrlDownloadToFile :: OnProgress总是返回具有相同值的ulProgress和ulProgressMax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆