德尔福 "IdHTTP.Request.Range"属性 : undeclared identifier

标签 delphi download resume undeclared-identifier

我正在寻找一些教程/源代码来恢复暂停/中止的下载。 我找到了源代码,但收到此错误:

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'

我该如何解决这个问题?

最佳答案

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() 可能会因错误而失败,或者无论您指定的范围如何,都会向您发送整个文件。

关于德尔福 "IdHTTP.Request.Range"属性 : undeclared identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9677326/

相关文章:

Delphi DBgrid 换行文字内容

c# - 当作为函数参数从 Delphi 传递到 C# 时,空字符串变为 null

delphi - 将具有不同通用类型的对象放在一起

delphi - Delphi .dproj 文件中的 <DCCReference> 标记有何作用?

javascript - 使用 Javascript 下载 .wav 或 .mp3

android - 我怎样才能恢复运行 Activity ?

wcf - WCF 是否支持任何类型的简历功能?

c# - web api 和 web api 2 之间的 PushStreamContent 有什么不同?

php - 通过ajax调用php下载文件

java - 通过通知栏恢复应用程序