[英]Delphi DataSnap upload xml file in body
在带有 Datasnap 组件的 delphi 10 中,我试图声明一个接收 XML 文件的 Post 方法,但我不能。
有人知道 Datasnap 是否只能在正文中接收 Json 格式类型吗?
(相反,任何例子都会很棒)
提前致谢。
您可以使用 datasnap WebModuleUnit 和自定义 ufileUploader 单元来克服这个问题,如下所示:
procedure TWebModule1.WebModuleDefaultAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
if Request.InternalPathInfo.StartsWith('/UploadFile') then
Response.Content := ufileUploader.UploadFile(Request)
else if (Request.InternalPathInfo = '') or (Request.InternalPathInfo = '/')
then
Response.Content := ReverseString.Content
else
Response.SendRedirect(Request.InternalScriptName + '/');
end;
unit ufileUploader;
interface
uses Web.HTTPApp;
function UploadFile(ARequest: TWebRequest): string;
implementation
uses System.SysUtils, System.Classes, Web.ReqMulti;
function UploadFile(ARequest: TWebRequest): string;
const
DestPath = 'c:\';
var
i: integer;
LFileName: string;
LStream: TMemoryStream;
begin
if not TMultipartContentParser.CanParse(ARequest) then
begin
Result := 'Cannot parse request';
Exit;
end;
if ARequest.Files.Count < 1 then
begin
Result := 'No file sended';
Exit;
end;
LStream := TMemoryStream.Create;
try
// You have sended ARequest.Files.Count files
for i := 0 to ARequest.Files.Count - 1 do
begin
LFileName := string(ARequest.Files.Items[i].FileName);
LStream.Clear;
// Read the sended file stream
LStream.CopyFrom(ARequest.Files.Items[i].Stream,
ARequest.Files.Items[i].Stream.Size);
LStream.Position := 0;
// Do what you want with the stream
LStream.SaveToFile(DestPath + LFileName);
end;
Result := Format('%d files saved to %s ', [ARequest.Files.Count, DestPath]);
finally
FreeAndNil(LStream);
end;
end;
end.
这不是您希望的答案,但我为这个确切的问题与 Embarcadero 开了一个案例,他们的回答是:
你好
我的名字是史蒂夫·阿克斯特尔。 我正在看这个案子。
很抱歉,Datasnap 不支持 XML。 它只支持 JSON,因此会出现错误消息。
问候
Steve Axtell Embarcadero 支持 ref:_00D30HwR._5005a28Y6yq:ref
问题出在Datasnap.DSService.TDSRESTService.ProcessParameters
中:
// Look for more parameters in the body
if (Content <> nil) and (Length(Content) > 0) then
begin
if LBody = nil then
begin
LBodyArray := nil;
LBody := TJSONObject.ParseJSONValue(Content, 0);
LFreeBody := LBody;
if LBody = nil then
begin
//ParamArray.Free;
raise TDSServiceException.Create(SNoJSONValue);
end;
if (LBody is TJSONObject) and (TJSONObject(LBody).Count = 1) and
(TJSONObject(LBody).Pairs[0].JSonString.Value = PARAMETERS_PAIR) and
(TJSONObject(LBody).Pairs[0].JsonValue is TJSONArray) then
begin
LBodyArray := TJSONArray(TJSONObject(LBody).Pairs[0].JsonValue);
LBodyArrayIndex := 0;
end
end;
end;
如果正文不在 JSON 中,则无法处理 REST 请求,而且我还没有找到强制 DataSnap 不在正文中查找其他参数的方法。
请注意,在我的例子中,我没有使用 TComponent 而是使用 TDataModule 作为服务器方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.