Using WinInet to identify total file size before downloading it

后端 未结 3 1736
我寻月下人不归
我寻月下人不归 2020-12-09 20:54

I got the source below from a third-party site explaining how to download a file from the internet using WinInet. I\'m not too familiar with API, and I took at

3条回答
  •  无人及你
    2020-12-09 21:45

    Answering the question of how to get a download size with WinInet. This is out of one of my file downloaders that is based on WinInet.

    This is the method I use to get the download size:

    function TWebDownloader.GetContentLength(URLHandle: HINTERNET): Int64;
    // returns the expected download size.  Returns -1 if one not provided
       var
         SBuffer: Array[1..20] of char;
         SBufferSize: Integer;
         srv: integer;
       begin
         srv := 0;
        SBufferSize := 20;
        if HttpQueryInfo(URLHandle, HTTP_QUERY_CONTENT_LENGTH, @SBuffer, SBufferSize, srv) then
           Result := StrToFloat(String(SBuffer))
        else
           Result := -1;
       end;
    

    Use of this method requires an open request handle, and does NOT require reading any of the data:

     URLHandle := HttpOpenRequest(ConnectHandle, 'GET', Pchar(sitepath), nil,
                      nil, nil, INTERNET_FLAG_NO_CACHE_WRITE, 0);
     ...
     DownloadSize := GetContentLength(URLHandle);
    

    HTH

提交回复
热议问题