2010-06-03 16 views
5

Attualmente sto usando il componente TIdHTTP per scaricare un file da internet. Mi chiedo se è possibile mettere in pausa e riprendere il download utilizzando questo componente o forse un altro componente indy.Scaricare, sospendere e riprendere un download utilizzando i componenti di Indy

questo è il mio codice corrente, questo funziona ok per scaricare un file (senza curriculum), ma. ora voglio mettere in pausa il download chiudere la mia app, e quando la mia app si riavvia, riprendi il download dall'ultima posizione salvata.

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; 

risposta

1

Forse l'intestazione HTTP RANGE può aiutarti qui. Dai un'occhiata a http://www.west-wind.com/Weblog/posts/244.aspx per maggiori informazioni sulla ripresa dei download HTTP.

Guarda anche qui https://forums.embarcadero.com/message.jspa?messageID=219481 per la discussione relativa a TIdHTTP sullo stesso argomento.

+0

Il link al Embarcadero forum non è più valida :-( – Brendan

+0

è non riesco a trovare più lo stesso articolo, spero che la risposta qui sotto è sufficiente a dare risposte a questa domanda. –

5

il codice seguente ha funzionato per me. Si scarica il file da pezzi:

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; 
Problemi correlati