http OnProgress event
Return to Introduction  Previous page  Next page
Applies to
WinHTTP component.  

  TWinHTTPProgressEvent = procedure(Sender: TObject;  
    const ContentType: String; FileSize, BytesRead, ElapsedTime,  
    EstimatedTimeLeft: Integer; PercentsDone: Byte;  
    TransferRate: Single; Stream: TStream) of object;  
property OnProgress: TWinHTTPProgressEvent;  

The OnProgress event occurs every time when component has successfully downloaded part of data received in response to HTTP query.  
Write OnProgress event handler to show the download progress (PercentsDone parameter indicates the progress in percents) or handle part of data which already received (Stream parameter). Also, the WinHTTP component automatically calculate elapsed and estimated time before finishing download and speed of data transfer.  
The WinHTTP passes to the OnProgress event handler following parameters:  
ContentTypethe MIME-type of received data. For example if you downloaded usual text-file, the ContentType will be "text/plan". For HTML page ContentType will "text/html", for executable file — "application/binary" and "image/jpeg" for JPEG, JPG and JPE files.  
For more information about Internet media types, please read RFC 2045, 2046, 2047, 2048, and 2077. Check out also the Internet media type registry at;  
FileSizetotal size of data which we currently downloading, in bytes (if possible to determinate).  
Note: usually server does not provide information about content-length for non-binary data (i.e: for "text/html" or "text/plain" content types);  
BytesReadsize of already received data, in bytes;  
ElapsedTimetime elapsed from beginning of download (in seconds);  
EstimatedTimeLeftestimated time left before finishing of download (Formula:  
X := FileSize / BytesRead * ElapsedTime - ElapsedTime);  
PercentsDoneprogress in percents (0%..100%);  
TransferRatespeed of data transfer (in Kb/s);  
Streamstream which contains already downloaded data. (Check out description of TStream, TMemoryStream and TFIleStream classes for more info).  

procedure TForm1.HTTP1Progress(Sender: TObject; ContentType: String; FileSize, BytesRead, ElapsedTime, EstimatedTimeLeft: Integer; PercentsDone: Byte; TransferRate: Single; Stream: TStream);  
  // progress bar position  
  CurrentFileProgressBar.Position := PercentsDone;  
  // file size  
  FileSizeLabel.Caption :=   
       Format('File size: %.1f Kb', [FileSize / 1024]);  
  // downloaded (in Kb)  
  DownloadedLabel.Caption :=  
      Format('Downloaded: %.1f Kb:', [BytesRead / 1024]);  
  // transfer rate  
  TransRateLabel.Caption :=   
       Format('Transfer rate: %.1f Kb/s', [TransferRate]);  
  // estimated time left  
  EstTimeLeftLabel.Caption :=   
       Format('Estimated time left: %d:%.2d:%.2d',  
          [EstimatedTimeLeft div 60 div 60,   // hours  
           EstimatedTimeLeft div 60 mod 60,   // minutes  
           EstimatedTimeLeft mod 60 mod 60]); // seconds  

C++ Builder:

void __fastcall TForm1::WinHTTP1Progress(TObject *Sender,  
      AnsiString ContentType, int FileSize, int BytesRead,  
      int ElapsedTime, int EstimatedTimeLeft,  
      BYTE PercentsDone, float TransferRate, TStream *Stream)  
  // progress bar position  
  ProgressCurrentFile->Position = PercentsDone;  
  // file size  
  FileSizeLabel->Caption =  
      Format("File size: %.1f Kb:",   
      ARRAYOFCONST(((float)FileSize / 1024)));  
  // downloaded (in Kb)  
  DownloadedLabel->Caption =  
      Format("Downloaded: %.1f Kb:",  
      ARRAYOFCONST(((float)BytesRead / 1024)));  
  // transfer rate  
  TransRateLabel->Caption =  
      Format("Transfer rate: %.1f Kb/s",  
  // estimated time left  
  EstTimeLeftLabel->Caption =  
      Format("Estimated time left: %d:%.2d:%.2d",  
      ARRAYOFCONST((EstimatedTimeLeft / 60 / 60,    // hours  
                    EstimatedTimeLeft / 60 % 60,    // minutes  
                    EstimatedTimeLeft % 60 % 60))); // seconds  

See also
OnDone event.