简单的说现在我传输过去的是如果是txt文件!打开就是乱码!但感觉里面的数字没变!别的传过去只是大小一样!文件打不开或者是乱码?
cliect  文件传送端
procedure DownLoadFileProc(AThread: TIdPeerThread);
var
  TheFileName: string;
  ThePosition: integer;
  FromF: file of byte;
  FileLen: integer;begin
  try
    TheFileName := AThread.Connection.ReadLn(EOL);
    ThePosition := AThread.Connection.ReadInteger;
  except
    AThread.Connection.Disconnect;
    exit;
  end;
  if FileExists(TheFileName) then
  begin
      DownLoadThreadExecute(AThread,TheFileName, ThePosition);
  end
  else
  begin
    try
      AThread.Connection.Write('文件不存在' + EOL);
      AThread.Connection.Disconnect;
    except
      AThread.Connection.Disconnect;
    end;
  end;
end;procedure DownLoadThreadExecute(AThread: TIdPeerThread; TheFileName: string; ThePosition:integer);
var
  FromF: file ;
  FileLen,BufferTotalNum: integer;
  NumRead, NumWritten: Integer;
  Buf: array[1..32768] of Char;
  tmStr:Tmemorystream;
begin
  try
    AssignFile(FromF, TheFileName);
    FileMode := 0;
    Reset(FromF,1);
    FileLen := FileSize(FromF);
    Seek(FromF, ThePosition);
  except
    try
      CloseFile(FromF);
    except
    end;
    exit;
  end;
  try
    AThread.Connection.Write('OK'+inttostr(FileLen-ThePosition)+EOL);
  except
    CloseFile(FromF);
    exit;
  end;
    NumRead:=0;
    BufferTotalNum:=ThePosition;  repeat
    try
      AThread.Connection.OpenWriteBuffer;
      BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
      AThread.Connection.WriteBuffer(Buf, NumRead);
      AThread.Connection.CloseWriteBuffer;
      BufferTotalNum:=BufferTotalNum+NumRead;
    except
      CloseFile(FromF);
      exit;
    end;
  until (BufferTotalNum=FileLen);
  CloseFile(FromF);
  if AThread.Connection.Connected then
  AThread.Connection.Disconnect;end;
server端  文件接收端  try //发送下载的文件
      DownLoadTcp.Write('文件下载' + EOL); //1-------SendfileCMD
      DownLoadTcp.Write(SrcouceDir.Text + FileNameText.Text + EOL); //2-------SendfileName
    except
      DownLoadTcp.Disconnect;
      Animate1.Active := False;
      beep;
      Self.Caption := '提示:网络信息发送失败!';
      exit;
    end;
    TmpFName := DownLoadDir.Text + FileNameText.Text;
    //================================文件接续
    try
      if FileExists(TmpFName) then
      begin
        SaveFStream := TFileStream.Create(TmpFName, fmOpenReadWrite);
        AlreadyReadLen := SaveFStream.Size;
      end
      else
      begin
        SaveFStream := TFileStream.Create(TmpFName, fmCreate);
        AlreadyReadLen := 0;
      end;
    except
      beep;
      beep;
      Self.Caption := '错误提示:请重新指定下载目录!';
      exit;
    end;
    //===============================
    try
      DownLoadTcp.WriteInteger(AlreadyReadLen); //---send filePos文件位置
    except
      SaveFStream.free;
      Animate1.Active := False;
      DownLoadTcp.Disconnect;
      Self.Caption := '错误提示:网络信息发送失败!';
      exit;
    end;    try
      RetStr := DownloadTcp.ReadLn(EOL);      //OK
    except
      SaveFStream.free;
      DownLoadTcp.Disconnect;
      Animate1.Active := False;
      Self.Caption := '错误提示:远程网络断开,接收失败!';
      exit;
    end;    if copy(RetStr,1,2) = 'OK' then //------------------------读取接收标志
    begin //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      //读数据------------------------开始-----
     try 
          Delete(RetStr,1,2);
          ReadFileLen:=strtoint(RetStr);//文件大小
      except
        SaveFStream.free;
        DownLoadTcp.Disconnect;
        Animate1.Active := False;
        Self.Caption := '错误提示:远程网络断开,接收失败!';
        exit;
      end;
      SaveFStream.Position := SaveFStream.Size;
      ProgressBar1.Max := ReadFileLen + AlreadyReadLen;
      ProgressBar1.Position := 0;
      try
        DownLoadTcp.ReadStream(SaveFStream, ReadFileLen);
      except
        SaveFStream.Free;
        Animate1.Active := False;
        DownLoadTcp.Disconnect;
        Self.Caption := '错误提示:远程网络断开,接收失败!';
        exit;
      end;
      SaveFStream.free; //下载成功
      Bitbtn3.Enabled := True;
      beep;
      Self.Caption := '下载完毕! 共:' + IntToStr(ProgressBar1.Max) + '字节';
      ProgressBar1.Position := ProgressBar1.Max;
      Sleep(100);
      DownLoadTcp.Disconnect;
      Animate1.Active := False;
      if checkBox1.Checked then Close;
      exit;
    end;