gpt4 book ai didi

delphi - Delphi datasnap将图像从发送到服务器(存储在数据库中)

转载 作者:行者123 更新时间:2023-12-03 18:12:43 25 4
gpt4 key购买 nike

我在上下搜索了一个可行的解决方案,以使用datasnap方法将图像(例如tpngimage)发送到服务器-但我无法使其正常工作。

当我加载图像并将其保存到内存流时,我能够从流中读取图像-在客户端本地,这并不奇怪。但是,当调用服务器方法时,流为nil并且不包含任何内容,其他参数也很好(简单的数据类型和对象)。

我想念这里明显的东西吗?我的印象是TStream是datasnap方法的有效数据类型,但也许我错了吗?

来自客户端的看起来像这样。

function TPerson.Update: Boolean;
var
AStream : TMemoryStream;
APicture : TPngImage;
ASize : Integer;
begin
if (FId > 0) then // if Id below zero we have a problem
begin

ClientModule1.ServerMethods1Client.UpdatePerson(Self);
APicture := TPngImage.Create;
AStream := TMemoryStream.Create;
try
// Temp just use a file
AStream.LoadFromFile('.\images\075.png');
ASize := AStream.Size;
AStream.Position := 0; // wind back if needed

// just for testing, we can read back the image from the stream
APicture.LoadFromStream(AStream);

ClientModule1.ServerMethods1Client.UpdatePersonPicture(self, ASize, AStream);

finally
FreeAndNil(AStream);
FreeAndNil(APicture);
end;

end;
FModified := False;
end;


代理方法看起来像这样

procedure TServerMethods1Client.UpdatePersonPicture(APerson: TPerson; ASize: Integer; APictureStream: TMemoryStream);
begin
if FUpdatePersonPictureCommand = nil then
begin
FUpdatePersonPictureCommand := FDBXConnection.CreateCommand;
FUpdatePersonPictureCommand.CommandType := TDBXCommandTypes.DSServerMethod;
FUpdatePersonPictureCommand.Text := 'TServerMethods1.UpdatePersonPicture';
FUpdatePersonPictureCommand.Prepare;
end;
if not Assigned(APerson) then
FUpdatePersonPictureCommand.Parameters[0].Value.SetNull
else
begin
FMarshal := TDBXClientCommand(FUpdatePersonPictureCommand.Parameters[0].ConnectionHandler).GetJSONMarshaler;
try
FUpdatePersonPictureCommand.Parameters[0].Value.SetJSONValue(FMarshal.Marshal(APerson), True);
if FInstanceOwner then
APerson.Free
finally
FreeAndNil(FMarshal)
end
end;
FUpdatePersonPictureCommand.Parameters[1].Value.SetInt32(ASize);
FUpdatePersonPictureCommand.Parameters[2].Value.SetStream(APictureStream, FInstanceOwner);
FUpdatePersonPictureCommand.ExecuteUpdate;
end;


Server方法如下所示-由于APictureStream为nil,因此失败。

procedure TServerMethods1.UpdatePersonPicture(APerson: TPerson; ASize: integer;
APictureStream: TMemoryStream);
var
APicture : TPngImage;
begin
fdqPersons.Close;
fdqPersons.SQL.Clear;
fdqPersons.Connection.StartTransaction;
try
fdqPersons.SQL.Add('update Persons set Picture=:Picture ');
fdqPersons.SQL.Add('where Id=:Id');
fdqPersons.ParamByName('Id').Value := APerson.Id;

APicture := TPngImage.Create;
try
// APicture for testing - but APictureStream is nil!
APicture.LoadFromStream(APictureStream);
fdqPersons.ParamByName('Picture').Assign(APicture);
fdqPersons.ExecSQL;
finally
FreeAndNil(APicture);
end;
fdqPersons.Close;
fdqPersons.Connection.Commit;
LogEvent(format('Person picture updated ID: %d',[APerson.id]));
except
on e:exception do
begin
fdqPersons.Connection.Rollback;
LogEvent(format('Error updating person picture %s',[e.Message]));
raise;
end;
end;
end;

最佳答案

当您调用APicture.LoadFromStream(AStream);时,流的位置将移至末尾,因此,当将其传递到ClientModule1中时,该流将没有任何数据可读取。摆脱不必要的部分,将流写入TPngImage,或者在该部分之后将流的位置重置为0。

关于delphi - Delphi datasnap将图像从发送到服务器(存储在数据库中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35845709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com