2012-03-13 33 views
2

我正在尋找一些教程/源代碼來恢復暫停/中止下載。 我發現了源代碼,但我得到這個錯誤:德爾福「IdHTTP.Request.Range」屬性:未聲明標識符

procedure TForm1.Download(url, pathLocal : String); 
var 
    eFile  : TFileStream; 
    IdHTTP : TIdHTTP; 

begin 
    idHTTP := TIdHTTP.Create(nil); 

    if FileExists(pathLocal) then //Caso o arquivo já exista ele o abre, caso contrário cria um novo 
     eFile := TFileStream.Create(pathLocal,fmOpenReadWrite) 
    else 
     eFile := TFileStream.Create(pathLocal,fmCreate); 

    try 
     try 
     eFile.Seek(0,soFromEnd); //Colocando o ponteiro no final do arquivo 

     IdHTTP.Head(url); //Buscando informações do arquivo 

     if eFile.Position < IdHTTP.Response.ContentLength then //Somente se o arquivo já não foi totalmente baixado 
     begin 
      IdHTTP.Request.ContentRangeStart := eFile.Position; //Definindo onde deve inciar o download 
      IdHTTP.Request.ContentRangeEnd := IdHTTP.Response.ContentLength; //Verificando o tamanho do arquivo 

      if eFile.Position > 0 then 
      begin //É importante que o range seja definido com o tamanho inicial e o final 
       IdHTTP.Request.Range := Format('%d-%d',[eFile.Position,IdHTTP.Response.ContentLength]); 
      end; 

      IdHTTP.Get(url,eFile); 
     end; 
     except 
     ShowMessage('Conexão interrompida.'); 
     end; 
    finally 
     eFile.Free; 
     IdHTTP.Disconnect; 
     IdHTTP.Free; 
    end; 
end; 

這是錯誤:

Undeclared identifier: 'Range' 

我怎樣才能解決這個問題?

回答

3

ContentRange...性能不用於HTTP請求,只有HTTP響應。完全將他們從您的代碼中取出。只使用Range屬性(它存在於Indy 10中,因此請確保您沒有使用Indy 9或更早的版本)。至於Range屬性本身,您沒有正確格式化它。它需要一個bytes=前綴,你可以省略結束值告訴服務器你想要的文件的其餘部分:

IdHTTP.Request.Range := Format('bytes=%d-',[eFile.Position]); 

如果使用Ranges屬性來代替,它處理這些細節爲您(Range財產是不建議使用):

IdHTTP.Request.Ranges.Add.StartPos := eFile.Position; 

之前你發送一個遠程請求時,一定要檢查是否Head()設置Response.AcceptRanges屬性bytes第一,否則Get()可能失敗,錯誤或者不管您發送指定範圍的全部文件。