Recent

Author Topic: Synapse and getting http filesize  (Read 4867 times)

rainamnkk

  • Newbie
  • Posts: 3
Synapse and getting http filesize
« on: October 31, 2012, 10:40:42 am »
Hi,

I cant find the function for getting the size of a file i want to download.
I need it for my progressbar.

Code: [Select]
procedure TForm1.BGetClick(Sender: TObject);
begin

  Progressbar1.Position := 0;
  http := THTTPSend.Create;
  http.Sock.OnStatus := @SocketGetCallBack;
  TotalBytes := ???
  if http.HTTPMethod('GET', ELink.Text) then
    http.Document.SaveToFile('Sbot_update.rar');
  http.Free;
end;


procedure TForm1.SocketGetCallBack(Sender: TObject; Reason: THookSocketReason;
  const Value: string);
begin
  case Reason of
    HR_ReadCount:
    begin
      Inc(CurrentBytes, StrToIntDef(Value, 0));
      ProgressBar1.Position := Round(1000 * (CurrentBytes / TotalBytes));
    end;
    HR_Connect: CurrentBytes := 0;
  end;
end;

In my procedure BGet i want to fill "TotalBytes" with the value of the file from the URL.
Does anyone know how to get it? In FTP its easy: Filesize(URL) but for Http ... -.-

-Danny

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Synapse and getting http filesize
« Reply #1 on: October 31, 2012, 02:10:55 pm »
Look in http.Headers for 'Content-Length:'. The character case may vary.

EDIT: If you don't want to look up 'Content-Length:' in the callback, you can do a
Code: [Select]
http.HTTPMethod('HEAD', ELink.Text) before the GET to retrieve only the headers.
« Last Edit: October 31, 2012, 02:16:16 pm by ludob »

rainamnkk

  • Newbie
  • Posts: 3
Re: Synapse and getting http filesize
« Reply #2 on: October 31, 2012, 04:10:34 pm »
Thanks for your answer :)

From the point that this is my first time using synapse and especially this kind of programming i have no clue how to work with your reply.

could you give me an code example, how to fill the var TotalBytes with the value of content-length?

Thanks
-Danny

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Synapse and getting http filesize
« Reply #3 on: October 31, 2012, 06:35:54 pm »
Here you go:

Code: [Select]
procedure TForm1.BGetClick(Sender: TObject);
var i:integer;
  s:string;
begin
  Progressbar1.Position := 0;
  http := THTTPSend.Create;
  http.Sock.OnStatus := @SocketGetCallBack;
  if http.HTTPMethod('HEAD', ELink.Text) then
    begin
    // search for "Content-Length:" in header
    for i:=0 to http.Headers.Count-1 do
      begin
      s:=UpperCase(http.Headers[i]);
      if pos('CONTENT-LENGTH:',s)>0 then
        TotalBytes := StrToIntDef(copy(s,pos(':',s)+1,length(s)),0);
      end;
    // get the file
    http.Headers.Clear;
    if http.HTTPMethod('GET', ELink.Text) then
      http.Document.SaveToFile('Sbot_update.rar');
    end;
  http.Free;
end;


procedure TForm1.SocketGetCallBack(Sender: TObject; Reason: THookSocketReason;
  const Value: string);
begin
  if TotalBytes=0 then
    exit;
  case Reason of
    HR_ReadCount:
    begin
      Inc(CurrentBytes, StrToIntDef(Value, 0));
      ProgressBar1.Position := Round(1000 * (CurrentBytes / TotalBytes));
    end;
    HR_Connect: CurrentBytes := 0;
  end;
end;
When "Content_length:" can't be found, TotalBytes =0 and SocketGetCallBack returns immediately. Also to avoid a divide by 0.

rainamnkk

  • Newbie
  • Posts: 3
Re: Synapse and getting http filesize
« Reply #4 on: October 31, 2012, 07:23:58 pm »
Thank you very much :) Now, everything is working!!

Found some mistakes ... multiply by 1000 ?? 100 was correct :D

thanks for your help!

 

TinyPortal © 2005-2018