如何使用一个TCP-Server从两个不同的位置发送文件?

时间:2015-01-14 20:41:49

标签: file delphi tcp

如何通过一个TCP-Server从两个不同的位置发送文件,我设法只从一个位置发送文件。 这个代码从一个目录发送......

procedure TForm1.Timer1Timer(Sender: TObject);
var
  fs: TFileStream;
  fn: string;
  sr: TSearchRec;
  I: integer;
begin
  I := 0;
  if FindFirst('C:/*.jpg', faAnyFile, sr) = 0 then
  begin
    with StringGrid1 do
    begin
      ListBox1.Items.Add('C:/' + sr.Name);
      while FindNext(sr) = 0 do
      begin
        ListBox1.Items.Add('C:/' + sr.Name);
        Inc(I);
        if I = 7 then
          Break;
      end;
      FindClose(sr);
      idTCPClient1.Connect;
      for fn in ListBox1.Items do
      begin
        fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyWrite);
        try
          idTCPClient1.IOHandler.WriteLn(ExtractFileName(fn));
          idTCPClient1.IOHandler.Write(fs, 0, True);
          idUDPClient1.Send(lbLatitude.Text + ',' + lbLongitude.Text);
        Finally
          fs.Free;
        end;
      end;
    end;
  end;
end;

1 个答案:

答案 0 :(得分:0)

所有你必须做的事情(没有像DavidH建议的那样完全重写你的代码)只是用你想要的所有不同目录的路径填充你的ListBox,例如:

procedure TForm1.Timer1Timer(Sender: TObject);
var
  fs: TFileStream;
  fn: string;
  sr: TSearchRec;
  I : integer;
begin
  I := 0;
  if FindFirst('C:/*.jpg', faAnyFile, sr) = 0 then
  begin
    repeat
      ListBox1.Items.Add('C:/' + sr.Name);
      Inc(I);
      if I = 7 then Break;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;

  if I < 7 then
  begin
    if FindFirst('C:/Some Other Folder/*.jpg', faAnyFile, sr) = 0 then
    begin
      repeat
        ListBox1.Items.Add('C:/Some Other Folder/' + sr.Name);
        Inc(I);
        if I = 7 then Break;
      until FindNext(sr) <> 0;
      FindClose(sr);
    end;
  end;

  idTCPClient1.Connect;
  for fn in ListBox1.Items do
  begin
    fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyWrite);
    try
      IdTCPClient1.IOHandler.WriteLn(ExtractFileName(fn));
      IdTCPClient1.IOHandler.Write(fs, 0, True);
      ...
    finally
      fs.Free;
    end;
  end;
end;