简体   繁体   English

Delphi DataSnap 在正文中上传 xml 文件

[英]Delphi DataSnap upload xml file in body

in delphi 10 with the Datasnap component I am trying to declare a Post method that receives an XML file but I can't.在带有 Datasnap 组件的 delphi 10 中,我试图声明一个接收 XML 文件的 Post 方法,但我不能。

Does anybody know if Datasnap only can receive Json format type in the body?有人知道 Datasnap 是否只能在正文中接收 Json 格式类型吗?

(in contrary any example will be great) (相反,任何例子都会很棒)

Thanks in advance.提前致谢。

You can overcome this with your datasnap WebModuleUnit and your custom ufileUploader unit like this :您可以使用 datasnap WebModuleUnit 和自定义 ufileUploader 单元来克服这个问题,如下所示:

  1. In WebModuleUnit :在 WebModuleUnit 中:
    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;
  1. in your ufileUploader unit :在您的 ufileUploader 单元中:
    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.

This is not the answer you are hoping for, but I opened a case with Embarcadero for this exact problem and their response was:这不是您希望的答案,但我为这个确切的问题与 Embarcadero 开了一个案例,他们的回答是:

Hello你好

My name is Steve Axtell.我的名字是史蒂夫·阿克斯特尔。 I am looking at this case.我正在看这个案子。

I am sorry but Datasnap does not support XML.很抱歉,Datasnap 不支持 XML。 It only supports JSON, hence the error message.它只支持 JSON,因此会出现错误消息。

Regards问候

Steve Axtell Embarcadero Support ref:_00D30HwR._5005a28Y6yq:ref Steve Axtell Embarcadero 支持 ref:_00D30HwR._5005a28Y6yq:ref

The problem is in Datasnap.DSService.TDSRESTService.ProcessParameters :问题出在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;

If the body is not in JSON, it fails to process the REST request, and I've not found a way to force DataSnap to not look in the body for additional parameters.如果正文不在 JSON 中,则无法处理 REST 请求,而且我还没有找到强制 DataSnap 不在正文中查找其他参数的方法。

Note that in my case, I'm not using TComponent but TDataModule for the server methods.请注意,在我的例子中,我没有使用 TComponent 而是使用 TDataModule 作为服务器方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM