Function TServerMethods.DownLoadFile(AfileName: string;Var FileSize:Int64):TStream ;
var
  ALLpath: string;
  tmpFile:TFileStream;
begin
  Result:=nil;
try
try
  ALLpath := RootPath + FilePath + AfileName;
  if FileExists(ALLpath) then
  begin
    tmpFile := TFileStream.Create(ALLpath, fmOpenRead);
    FileSize:=tmpFile.Size;
    tmpFile.Position:=0;
    Result:=TMemoryStream.Create;
    Result.CopyFrom(tmpFile,0);
    Result.Position:=0;
    FreeAndNil(tmpFile);
  end
  else
    FileSize:=-2;
Except
  FileSize:=-1;
  FreeAndNil(tmpFile);
  FreeAndNil(Result);
end;
finally
  //FreeAndNil(Result);
end;
//此处也可以加错误处理 或 释放默认文件 再生成流
end;function TServerMethods.PutFile(ProgCallback: TDBXCallback;Params:TParams; Stream: TStream):Integer;
const BufSize = $F000;
var
  tmpSize:Int64;
  Buffer: TBytes;
  ReadCount: Integer;
  FS: TFileStream;
  AfileName:String;
  SQL,TimeFile:String;
  DBXTransaction:TDBXTransaction;
begin
  tmpSize:=0;
  Result:=-1;
  if not DirectoryExists(RootPath + FilePath) then
    ForceDirectories(RootPath + FilePath);
  if not DirectoryExists(RootPath + BackPath) then
    ForceDirectories(RootPath + BackPath);
    try
       try
        if FileExists(RootPath + FilePath + AfileName) then
        begin
          MoveFile(PChar(RootPath + FilePath + AfileName),PChar(RootPath + BackPath + AfileName+'@'+TimeFile));
        end;
      except
      end;
      FS := TFileStream.Create(RootPath + FilePath + AfileName, FmCreate);
      if Stream.Size = -1 then // 大小未知则一直读取到没有数据为止
      begin
          SetLength(Buffer, BufSize);
          repeat
            ReadCount := Stream.Read(Buffer[0], BufSize);
            tmpSize:=tmpSize+ReadCount;
            ProgCallback.Execute(TJSONNumber.Create(tmpSize)).Free;
            if ReadCount > 0 then
              FS.WriteBuffer(Buffer[0], ReadCount);
            if ReadCount < BufSize then
              break;
          until ReadCount < BufSize;
      end
      else
          FS.CopyFrom(Stream, 0);
      ProgCallback.Execute(TJSONNumber.Create(FS.Size)).Free;
      FreeAndNil(Fs);
      SQLConnection1.CommitFreeAndNil(DBXTransaction);
      Result:=0;
  Except
    if FS<>nil then
      FreeAndNil(Fs);
    if FileExists(RootPath + FilePath + AfileName) then
      DeleteFile(RootPath + FilePath + AfileName);
  end;
end;
end;正常上传下载都没有问题,但只要客户端调用这两个过程中的任何一个,客户端关闭连接就会出错
F1:=MyServer.DownBackFile(sFile.FileID,BackTime,DownOk)
MyServer.PutFile(tmpProgCallback,Param,Stream)

解决方案 »

  1.   

    应该是的,只要用到Tstream就出错,但释放也错
      

  2.   

     FileSize:=tmpFile.Size;   //tmpFile.Size应该是string类型或integer类型吧?
      

  3.   

    文件大小我查了一下,都是Int64的吧,就是上面的代码
      

  4.   

    怎么清空?我用的是局部变量,free就报错
      

  5.   

    DownLoadFile 中
    tmpFile  不用释放 ,默认调用 TmpFile 是有datasnap管理的 ,他会自己释放的PutFile 中 你客户端 调用时 ,上传的 Stream: TStream 也不用自己释放
      

  6.   

    tmpFile := TFileStream.Create(ALLpath, fmOpenRead);
      FileSize:=tmpFile.Size;
      tmpFile.Position:=0;
      Result:=TMemoryStream.Create;
      Result.CopyFrom(tmpFile,0);
      Result.Position:=0;
      FreeAndNil(tmpFile);
    这里释放有问题?已经CopyFrom了,不可以释放吗?