Using WinInet to identify total file size before downloading it

后端 未结 3 1737
我寻月下人不归
我寻月下人不归 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:36

    after fixing the types it looks better like this:

    function GetContentLength(URLHandle:HINTERNET):Int64;
    // returns the expected download size.  Returns -1 if one not provided
    var
     SBufferSize, srv:Cardinal;
    begin
     srv:=0;
     SBufferSize:=20;
     if Not HttpQueryInfo(URLHandle, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, {@SBuffer} @Result, SBufferSize, srv) then Result:=-1;
    end;
    

    to call it:

    {get the file handle}
    hURL:=InternetOpenURL(hSession, PChar(URL), nil, 0, 0, 0);
    if hURL=Nil then
    begin
     InternetCloseHandle(hSession);
     ShowMessage('The link is incorrect!');
     exit;
    end;
    {get the file size}
    filesize:=GetContentLength(hURL);
    

提交回复
热议问题