Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:49:06+00:00 2026-05-29T09:49:06+00:00

I got the source below from a third-party site explaining how to download a

  • 0

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 look at the WinInet unit but did not see any API calls like what I need.

What I’m doing is adding the ability to report back progress of downloading a file. This procedure I’ve already wrapped inside of a TThread and everything works fine. However, just one missing piece: Finding the total size of the source file before downloading.

See below where I have a comment //HOW TO GET TOTAL SIZE? This is where I need to find out what is the total size of the file BEFORE I begin downloading it. How do I go about doing this? Because this code seems to not know the size of the file until it’s done being downloaded – and that makes this addition irrelevant.

procedure TInetThread.Execute;
const
  BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: array[1..BufferSize] of Byte;
  BufferLen: DWORD;
  f: File;
  S: Bool;
  D: Integer;
  T: Integer;
  procedure DoWork(const Amt: Integer);
  begin
    if assigned(FOnWork) then
      FOnWork(Self, FSource, FDest, Amt, T);
  end;
begin
  S:= False;
  try
    try
      if not DirectoryExists(ExtractFilePath(FDest)) then begin
        ForceDirectories(ExtractFilePath(FDest));
      end;
      hSession:= InternetOpen(PChar(FAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
      try
        hURL:= InternetOpenURL(hSession, PChar(FSource), nil, 0, 0, 0);
        try
          AssignFile(f, FDest);
          Rewrite(f, 1);
          T:= 0; //HOW TO GET TOTAL SIZE?
          D:= 0;
          DoWork(D);
          repeat
            InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
            BlockWrite(f, Buffer, BufferLen);
            D:= D + BufferLen;
            DoWork(D);
          until BufferLen = 0;
          CloseFile(f);
          S:= True;
        finally
          InternetCloseHandle(hURL);
        end
      finally
        InternetCloseHandle(hSession);
      end;
    except
      on e: exception do begin
        S:= False;
      end;
    end;
  finally
    if assigned(FOnComplete) then
      FOnComplete(Self, FSource, FDest, S);
  end;
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:49:07+00:00Added an answer on May 29, 2026 at 9:49 am

    You can use the HEAD method and check the Content-Length to retrieve the file size of a remote file

    Check these two Methods

    WinInet

    If you want execute a HEAD method you must use the HttpOpenRequest, HttpSendRequest and HttpQueryInfo WinInet functions .

    uses
     SysUtils,
     Windows,
     WinInet;
    
    function GetWinInetError(ErrorCode:Cardinal): string;
    const
       winetdll = 'wininet.dll';
    var
      Len: Integer;
      Buffer: PChar;
    begin
      Len := FormatMessage(
      FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_FROM_SYSTEM or
      FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_IGNORE_INSERTS or  FORMAT_MESSAGE_ARGUMENT_ARRAY,
      Pointer(GetModuleHandle(winetdll)), ErrorCode, 0, @Buffer, SizeOf(Buffer), nil);
      try
        while (Len > 0) and {$IFDEF UNICODE}(CharInSet(Buffer[Len - 1], [#0..#32, '.'])) {$ELSE}(Buffer[Len - 1] in [#0..#32, '.']) {$ENDIF} do Dec(Len);
        SetString(Result, Buffer, Len);
      finally
        LocalFree(HLOCAL(Buffer));
      end;
    end;
    
    
    procedure ParseURL(const lpszUrl: string; var Host, Resource: string);
    var
      lpszScheme      : array[0..INTERNET_MAX_SCHEME_LENGTH - 1] of Char;
      lpszHostName    : array[0..INTERNET_MAX_HOST_NAME_LENGTH - 1] of Char;
      lpszUserName    : array[0..INTERNET_MAX_USER_NAME_LENGTH - 1] of Char;
      lpszPassword    : array[0..INTERNET_MAX_PASSWORD_LENGTH - 1] of Char;
      lpszUrlPath     : array[0..INTERNET_MAX_PATH_LENGTH - 1] of Char;
      lpszExtraInfo   : array[0..1024 - 1] of Char;
      lpUrlComponents : TURLComponents;
    begin
      ZeroMemory(@lpszScheme, SizeOf(lpszScheme));
      ZeroMemory(@lpszHostName, SizeOf(lpszHostName));
      ZeroMemory(@lpszUserName, SizeOf(lpszUserName));
      ZeroMemory(@lpszPassword, SizeOf(lpszPassword));
      ZeroMemory(@lpszUrlPath, SizeOf(lpszUrlPath));
      ZeroMemory(@lpszExtraInfo, SizeOf(lpszExtraInfo));
      ZeroMemory(@lpUrlComponents, SizeOf(TURLComponents));
    
      lpUrlComponents.dwStructSize      := SizeOf(TURLComponents);
      lpUrlComponents.lpszScheme        := lpszScheme;
      lpUrlComponents.dwSchemeLength    := SizeOf(lpszScheme);
      lpUrlComponents.lpszHostName      := lpszHostName;
      lpUrlComponents.dwHostNameLength  := SizeOf(lpszHostName);
      lpUrlComponents.lpszUserName      := lpszUserName;
      lpUrlComponents.dwUserNameLength  := SizeOf(lpszUserName);
      lpUrlComponents.lpszPassword      := lpszPassword;
      lpUrlComponents.dwPasswordLength  := SizeOf(lpszPassword);
      lpUrlComponents.lpszUrlPath       := lpszUrlPath;
      lpUrlComponents.dwUrlPathLength   := SizeOf(lpszUrlPath);
      lpUrlComponents.lpszExtraInfo     := lpszExtraInfo;
      lpUrlComponents.dwExtraInfoLength := SizeOf(lpszExtraInfo);
    
      InternetCrackUrl(PChar(lpszUrl), Length(lpszUrl), ICU_DECODE or ICU_ESCAPE, lpUrlComponents);
    
      Host := lpszHostName;
      Resource := lpszUrlPath;
    end;
    
    function GetRemoteFileSize(const Url : string): Integer;
    const
      sUserAgent = 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101';
    
    var
      hInet    : HINTERNET;
      hConnect : HINTERNET;
      hRequest : HINTERNET;
      lpdwBufferLength: DWORD;
      lpdwReserved    : DWORD;
      ServerName: string;
      Resource: string;
      ErrorCode : Cardinal;
    begin
      ParseURL(Url,ServerName,Resource);
      Result:=0;
    
      hInet := InternetOpen(PChar(sUserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
      if hInet=nil then
      begin
        ErrorCode:=GetLastError;
        raise Exception.Create(Format('InternetOpen Error %d Description %s',[ErrorCode,GetWinInetError(ErrorCode)]));
      end;
    
      try
        hConnect := InternetConnect(hInet, PChar(ServerName), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
        if hConnect=nil then
        begin
          ErrorCode:=GetLastError;
          raise Exception.Create(Format('InternetConnect Error %d Description %s',[ErrorCode,GetWinInetError(ErrorCode)]));
        end;
    
        try
          hRequest := HttpOpenRequest(hConnect, PChar('HEAD'), PChar(Resource), nil, nil, nil, 0, 0);
            if hRequest<>nil then
            begin
              try
                lpdwBufferLength:=SizeOf(Result);
                lpdwReserved    :=0;
                if not HttpSendRequest(hRequest, nil, 0, nil, 0) then
                begin
                  ErrorCode:=GetLastError;
                  raise Exception.Create(Format('HttpOpenRequest Error %d Description %s',[ErrorCode,GetWinInetError(ErrorCode)]));
                end;
    
                 if not HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, @Result, lpdwBufferLength, lpdwReserved) then
                 begin
                  Result:=0;
                  ErrorCode:=GetLastError;
                  raise Exception.Create(Format('HttpQueryInfo Error %d Description %s',[ErrorCode,GetWinInetError(ErrorCode)]));
                 end;
              finally
                InternetCloseHandle(hRequest);
              end;
            end
            else
            begin
              ErrorCode:=GetLastError;
              raise Exception.Create(Format('HttpOpenRequest Error %d Description %s',[ErrorCode,GetWinInetError(ErrorCode)]));
            end;
        finally
          InternetCloseHandle(hConnect);
        end;
      finally
        InternetCloseHandle(hInet);
      end;
    
    end;
    

    Indy

    Also check this code using indy.

    function GetRemoteFilesize(const Url :string) : Integer;
    var
      Http: TIdHTTP;
    begin
      Http := TIdHTTP.Create(nil);
      try
        Http.Head(Url);
        result:= Http.Response.ContentLength;
      finally
        Http.Free;
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a source code file that started as a copy of some sample
The following query generates the error below in 10g: select DBMS_METADATA.GET_DDL('TABLE','TEST_TABLE','TEST') from dual; Got:
I first got an error usign the code below, explaining that DataGridLinkButton' must be
I've got a TreeView whose data context is set using LayoutRoot.DataContext = value from
I've got a Haskell program that needs to execute a separate (third party) binary;
I have got below xml in my string and I am using c# 2.0
I'm trying to use jQueryUI draggable/droppable/sortable to move (not copy) items from a source
I got a source for console program written in c++ for linux Is there
I am rather new to VIM. I got some source code and this is
I'm working on a hack to the DotNetNuke Events module. I've got the source

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.