Recent

Author Topic: How to prevent TIdHTTP resize FileStream  (Read 2651 times)

Fahmy Rofiq

  • Jr. Member
  • **
  • Posts: 83
How to prevent TIdHTTP resize FileStream
« on: April 07, 2013, 04:44:59 am »
This is the scenario:
I want to download a file, but only a part of it, lets say just the middle part, because I already download the file but it seems corrupt on the middle part,,,

Lets assume the file is 900MB, and I want to download about 100MB of the middle part, this is what I did:

Code: [Select]
procedure DownloadFile(URL, FilePath: string; StartOffset, EndOffset: int64);
var
  HTTP: TIdHTTP;
  FS: TFileStream;
begin
  HTTP:= TIdHTTP.Create(nil);
  FS:= TFileStream.Create(FilePath, fmOpenReadWrite);
  try
    FS.Seek(StartOffset, soFrom Beginning);
    HTTP.Request.Range := IntToStr(StartOffset) + '-' + IntToStr(EndOffset);
    HTTP.Get(URL,FS);
  finally
    FS.Free;
    HTTP.Free;
  end;
end;

procedure TfrmMain.btnDownloadClick(Sender: TObject);
begin 
  DownloadFile('http://url.com/file.zip','C:\AlreadyDownloadedFile.zip', 500000000, 600000000);
end;

The file is downloaded with the right range, but the problem is, TIdHTTP resize my 900MB downloaded file become same as the value of EndOffset (about 600MB). How to prevent this? How to tell TIdHTTP to just write the data received and not resize the FileStream?

Thanks,
Fahmy
Lazarus Trunk + FPC Fixes 32bit
Windows 10 x64

 

TinyPortal © 2005-2018