Synapse and getting http filesize lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- rainamnkk · 2012-10-31 09:40 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 · 2012-10-31 13:10 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. ---------------------------------------------------------------------- rainamnkk · 2012-10-31 15:10 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 · 2012-10-31 17:35 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 · 2012-10-31 18:23 Thank you very much Now, everything is working!! Found some mistakes ... multiply by 1000 ?? 100 was correct thanks for your help!