我正在使用此datasnap servermethod传输图像:
function TServerMethods.DownloadFile(sFilePath: string): TStream;
var
strFileStream: TFileStream;
begin
GetInvocationMetadata.ResponseContentType := 'image/jpeg';
strFileStream := TFileStream.Create(sFilePath, fmOpenRead);
Result := strFileStream;
end;
这样可行,但似乎创建的流永远不会被释放!当我尝试删除该文件时,我收到“文件正在使用错误”。当我返回一个自写类时,这是同样的问题;析构函数永远不会被调用。我必须启用某些功能吗?
答案 0 :(得分:0)
这就是我所做的修复它,它是解决方法,但至少可以工作 1.返回文件流的Instred返回TStream,我使用函数IsFileInUse
的修改 Files.IsFileInUse(path); //Check if file is used and try to free it
fs := TFileStream.Create(path, fmOpenRead or fmShareDenyNone);
fs.Position := 0;
RecordSize := SizeOf(Measurements);
StreamSize := fs.Size;
j := (StreamSize div RecordSize) - 1;
result := TMemoryStream.Create();
for i := 0 to j do
begin
result.CopyFrom(fs, RecordSize);
end;
FreeAndNil(fs);
//result := TFileStream.Create(path, fmOpenRead or fmShareDenyNone);
result.Position := 0;
2。在客户端应用程序第一次下载文件,但第二次它遇到异常。所以尝试exc里面尝试exc重复同样的调用2次。它看起来像下载文件调用上的1个调用rase异常,但它也是免费Stream所以每次第二次调用工作。
try
try
fs := TFileStream.Create(FilePath + RecordFileNameAPI, fmCreate);
st := TMemoryStream.Create();
st := APIClientModule.ServerMethodsBlob.getRecordingbyId(RecordFileNameAPI);
st.Position := 0;
RecordSize := SizeOf(Measurements);
StreamSize := st.Size;
j := (StreamSize div RecordSize) - 1;
setlength(MeasurementArrayAPI, j + 1);
// Copy Stream to memory object
for i := 0 to j do
begin
st.Read(Measurements, RecordSize);
MeasurementArrayAPI[i] := Measurements;
end;
// Copy Stream to File
st.Position := 0;
for i := 0 to j do
begin
fs.CopyFrom(st, RecordSize);
end;
Except
begin
try
FreeAndNil(fs);
FreeAndNil(st);
fs := TFileStream.Create(FilePath + RecordFileNameAPI, fmCreate);
st := TMemoryStream.Create();
st := APIClientModule.ServerMethodsBlob.getRecordingbyId(RecordFileNameAPI);
st.Position := 0;
RecordSize := SizeOf(Measurements);
StreamSize := st.Size;
j := (StreamSize div RecordSize) - 1;
setlength(MeasurementArrayAPI, j + 1);
// Copy Stream to memory object
for i := 0 to j do
begin
st.Read(Measurements, RecordSize);
MeasurementArrayAPI[i] := Measurements;
end;
// Copy Stream to File
st.Position := 0;
for i := 0 to j do
begin
fs.CopyFrom(st, RecordSize);
end;
Except
exc := true;
end;
end;
end;
finally
FreeAndNil(fs);
FreeAndNil(st);
if(not exc) then
begin
//APIClientModule.ServerMethodsBlob.completeDownloding();
if Assigned(FonGetLastRecordingDataAPIFinihed) then
FonGetLastRecordingDataAPIFinihed();
end;
end;