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)
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)
tmpFile 不用释放 ,默认调用 TmpFile 是有datasnap管理的 ,他会自己释放的PutFile 中 你客户端 调用时 ,上传的 Stream: TStream 也不用自己释放
FileSize:=tmpFile.Size;
tmpFile.Position:=0;
Result:=TMemoryStream.Create;
Result.CopyFrom(tmpFile,0);
Result.Position:=0;
FreeAndNil(tmpFile);
这里释放有问题?已经CopyFrom了,不可以释放吗?