-1

私は2つの文字列リストを持っています:name(ファイル名を含む)、url(ファイルurlを含む)。プログレスバーを使用して、THttpgetを1つのループで使用して必要なすべてのファイルをダウンロードしたいと思います。

for d:=0 to numberOfDownloads-1 do
begin
HTTPGet2.URL:=url[d];
HTTPGet2.FileName:=name[d];
HTTPGet2.GetFile;
end;

動作しますが、ダウンロードするファイルは1つだけです。最初は文字列リストです。どうやってやるの?numberOfDownloads-文字列リスト名のアイテムの数。

4

2 に答える 2

2

HTTPGetは、セカンダリスレッドを作成し、標準イベントを介して通知することにより、ファイルを非同期的にダウンロードします。

したがって、あなたの場合、ループを繰り返して2番目のファイルにアクセスしてダウンロードすると、HTTPGet2インスタンスは前のダウンロードの処理でまだビジー状態です。

これを克服する簡単な方法の1つは、HTTPGetインスタンスの配列を作成することです...このようなもの...

 var HTTPGets: array of THttpGet;
   ...
 setlength(HTTPGets,numberOfDownloads);
 ...
 for d:=0 to numberOfDownloads-1 do
   begin
    HTTPGets[d]:=THttpGet.Create(...);
    HTTPGets[d].URL:=...;
     ...
    HTTPGets[d].GetFile;
   end;
 end.

ファイナライズイベントの通知を受け取るには、独自のOnDoneFileを作成して設定する必要があります...

   HTTPGets[d].OnDoneFile:=HTTPGetOnDone;
于 2012-01-26T19:18:12.557 に答える
1

以下の概算コードは、Indyコンポーネントを使用する作業バージョンから抜粋したものです。コンパイルするにはいくつかのギャップを埋める必要がありますが、それはあなたにアイデアを与えるはずです。複数のファイルの場合は、ループのように繰り返し呼び出すか、ファイル名のリストを使用して呼び出し、以下のコードを内部でループさせます...

type
  TDownloadResult = (DRSuccess, DRHostNotFound, DRFileNotFound, DRUserCancelled, DROther);

function TDownload.Download(const aSourceURL: String;
                            const aDestFileName: String;
                            const aShowProgress: Boolean;
                            out   aDownloadResult: TDownloadResult;
                            out   aErrm: String): boolean;
var
  Stream: TMemoryStream;
  IDAntiFreeze: TIDAntiFreeze;
begin
  Screen.Cursor := crHourGlass;
  aDownloadResult := DROther;
  aErrm :='Unexpected web error.';
  fShowProgress := aShowProgress;
  if fShowProgress then
    begin
      frmProgressBar := TfrmProgressBar.Create(Application.MainForm);
      frmProgressBar.SetMessage1'Downloading File...');
      frmProgressBar.Show;
    end;

  fIDHTTP := TIDHTTP.Create;
  fIDHTTP.HandleRedirects := TRUE;
  fIDHTTP.AllowCookies := FALSE;
  fIDHTTP.Request.UserAgent := 'Mozilla/4.0';
  fIDHTTP.Request.Connection := 'Keep-Alive';
  fIDHTTP.Request.ProxyConnection := 'Keep-Alive';
  fIDHTTP.Request.CacheControl := 'no-cache';
  fIDHTTP.OnWork := IdHTTP1Work;
  fIDHTTP.OnWorkBegin := IdHTTP1WorkBegin;
  IDAntiFreeze := TIDAntiFreeze.Create;

  Stream := TMemoryStream.Create;
  try
    try
      fIDHTTP.Get(aSourceURL, Stream);
      if FileExists(aDestFileName) then
        DeleteFile(PWideChar(aDestFileName));
      Stream.SaveToFile(aDestFileName);
      Result := TRUE;
      aDownloadResult :=drSuccess;
    except
      On E: Exception do
        begin
          Result := FALSE;
          aErrm := E.Message + ' (' + IntToStr(fIDHTTP.ResponseCode) + ')';
          if fShowProgress AND fShowProgress AND frmProgressBar.Cancelled then
            aDownloadResult := DRUserCancelled
          else if (fIDHTTP.ResponseCode = 404) OR (fIDHTTP.ResponseCode = 302) then
            aDownloadResult := DRFileNotFound
          else if (FIDHTTP.ResponseCode = -1) then
            aDownloadResult := DRHostNotFound
          else
            aDownloadResult := DROther;
        end;
    end;
  finally
    Screen.Cursor := crDefault;
    Stream.Free;
    IDAntiFreeze.Free;
    fIDHTTP.Free;
    if fShowProgress then
      frmProgressBar.Free;
  end;
end;  { Download }


procedure TDownload.IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
  if fShowProgress AND frmProgressBar.Cancelled then
     raise EUserCancelled.Create('');
  if fShowProgress AND (fMaxWork > 0) then
    frmProgressBar.SetPosition(AWorkCount, GetMsg(MSG_PERCENT_COMPLETE, IntToStr( Round(AWorkCount/fMaxWork * 100)) + '%'));
end; { IdHTTP1Work }

procedure TDownload.IdHTTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
begin
  fMaxWork := AWorkCountMax;
  if fShowProgress then
    frmProgressBar.SetMinMax(0, fMaxWork);
end; { IdHTTP1WorkBegin }

使用例:

procedure TForm1.Button1Click(Sender: TObject);
var
  Download: TDownload;
  Errm: String;
  DownloadResult: TDownloadResult;
begin
  Download := TDownload.Create;
  if NOT TDownload.Download( 'http://www.URL.com/filename.ext',
                             'c:\junk\au.mpg', TRUE, DownloadResult, Errm) then
    begin
      case DownloadResult of
         DRFileNotFound: ShowMessage ('File not found!!' + #13 + Errm);
         DRUserCancelled: ShowMessage ('Cancelled!');
         DrHostNotFound: ShowMessage ('Are you on line?');
         DrOther: ShowMessage ('Other: ' + Errm);
      else
        ShowMessage('huh?');
      end
    end
  else
    ShowMessage ('Download succeeded!');
end;
于 2012-01-26T19:10:59.387 に答える