2010-06-03 24 views
5

Actualmente estoy usando el componente TIdHTTP para descargar un archivo de internet. Me pregunto si es posible pausar y reanudar la descarga usando este componente o tal vez otro componente indy.Descargar, pausar y reanudar una descarga usando los componentes de Indy

este es mi código actual, esto funciona bien para descargar un archivo (sin currículum), pero. ahora quiero pausar la descarga para cerrar mi aplicación, y cuando mi aplicación se reinicie, reanude la descarga desde la última posición guardada.

var 
    Http: TIdHTTP; 
    MS : TMemoryStream; 
begin 
    Result:= True; 
    Http := TIdHTTP.Create(nil); 
    MS := TMemoryStream.Create; 
    try 

    try 
     Http.OnWork:= HttpWork;//this event give me the actual progress of the download process 
     Http.Head(Url); 
     FSize := Http.Response.ContentLength; 
     AddLog('Downloading File '+GetURLFilename(Url)+' - '+FormatFloat('#,',FSize)+' Bytes'); 
     Http.Get(Url, MS); 
     MS.SaveToFile(LocalFile); 
    except 
     on E : Exception do 
     Begin 
     Result:=False; 
     AddLog(E.Message); 
     end; 
    end; 
    finally 
    Http.Free; 
    MS.Free; 
    end; 
end; 

Respuesta

1

Tal vez la cabecera HTTP GAMA puede ayudar aquí. Eche un vistazo al http://www.west-wind.com/Weblog/posts/244.aspx para obtener más información sobre cómo reanudar las descargas HTTP.

También vea aquí https://forums.embarcadero.com/message.jspa?messageID=219481 para discusión relacionada con TIdHTTP sobre el mismo tema.

+0

El enlace al foro Embarcadero ya no es válido :-( – Brendan

+0

es que no puedo encontrar el mismo artículo más, espero que la respuesta a continuación es suficiente para dar respuestas a esta pregunta. –

5

me funcionó el siguiente código. Es capaz de descargar el archivo por trozos:

procedure Download(Url,LocalFile:String; 
    WorkBegin:TWorkBeginEvent;Work:TWorkEvent;WorkEnd:TWorkEndEvent); 
var 
    Http: TIdHTTP; 
    exit:Boolean; 
    FLength,aRangeEnd:Integer; 
begin 
    Http := TIdHTTP.Create(nil); 
    fFileStream:=nil; 
    try 

    try 
     Http.OnWork:= Work; 
     Http.OnWorkEnd := WorkEnd; 

     Http.Head(Url); 
     FLength := Http.Response.ContentLength; 
     exit:=false; 
     repeat 

     if not FileExists(LocalFile) then begin 
      fFileStream := TFileStream.Create(LocalFile, fmCreate); 
     end 
     else begin 
      fFileStream := TFileStream.Create(LocalFile, fmOpenReadWrite); 
      exit:= fFileStream.Size >= FLength; 
      if not exit then 
      fFileStream.Seek(Max(0, fFileStream.Size-4096), soFromBeginning); 
     end; 

     try 
      aRangeEnd:=fFileStream.Size + 50000; 

      if aRangeEnd < fLength then begin   
      Http.Request.Range := IntToStr(fFileStream.Position) + '-'+ IntToStr(aRangeEnd); 
      end 
      else begin 
      Http.Request.Range := IntToStr(fFileStream.Position) + '-'; 
      exit:=true; 
      end; 

      Http.Get(Url, fFileStream); 
     finally 
      fFileStream.Free; 
     end; 
    until exit; 
    Http.Disconnect; 

    except 
     on E : Exception do 
     Begin 
     //Result:=False; 
     //AddLog(E.Message); 
     end; 
    end; 
    finally 
    Http.Free; 
    end; 
end; 
Cuestiones relacionadas