Json Base64在Delphi中编码时会断线

Json Base64在Delphi中编码时会断线,json,delphi,Json,Delphi,我使用delphi中的以下代码段将图像编码为Base64 procedure TWM.WMactArquivosAction(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); var ImagePath: string; JsonObject: TJSONObject; inStream, outStream: TStream; StrList: TStr

我使用delphi中的以下代码段将图像编码为
Base64

procedure TWM.WMactArquivosAction(Sender: TObject; Request: TWebRequest;
  Response: TWebResponse; var Handled: Boolean);
var
  ImagePath: string;
  JsonObject: TJSONObject;
  inStream, outStream: TStream;
  StrList: TStringList;
begin
  inStream := TFileStream.Create(ImagePath, fmOpenRead);
  try
    outStream := TFileStream.Create('final_file', fmCreate);
    JsonObject := TJSONObject.Create;
    try
      TNetEncoding.Base64.Encode(inStream, outStream);
      outStream.Position := 0;
      StrList := TStringList.Create;
      StrList.LoadFromStream(outStream);

      JsonObject.AddPair('file', StrList.Text);
    finally
      Response.Content := JsonObject.ToString;
      outStream.Free;
      JsonObject.DisposeOf;
    end;
  finally
    inStream.Free;
  end;
end;
它工作正常,文件被转换为
Base64
,并添加到
JsonObject

问题是,当从Web服务器检索此
JsonObject
时,我得到了错误的json格式,因为base64字符串中有换行符

你可以看到红色的那根是绳子。在第一个换行符之后,json受到干扰,并以蓝色显示,这意味着json响应中存在错误

问题

因此,问题在于,当编码到
Base64
时,它会在字符串中添加换行符,这在
Json
中不受支持

我猜

我有一个猜测,这确实有效,但我不确定这是最好的解决方案

我遍历了
TStringList
中的所有
字符串
,并将数据添加到
TStringBuilder
中。毕竟,我将
TStringBuilder
添加到了
Json
。看看我的代码

...
var
  ...
  StrBuilder: TStringBuilder;
begin
  ...
    try
      ...
      StrList.LoadFromStream(outStream);

      // New
      StrBuilder := TStringBuilder.Create;
      for I := 0 to StrList.Count - 1 do
        StrBuilder.Append(StrList.Strings[I]);

      JsonObject.AddPair('file', StrBuilder.ToString);
    finally
      Response.Content := JsonObject.ToString;
      ...
    end;
  ...
end;

正如您所看到的,JSON现在很好

问题

  • 循环浏览所有项目对我来说似乎是一个糟糕的解决方案,它能正常工作吗?(它在本地主机上以344ms的速度获得响应)
  • 有更好的解决办法吗

  • 代替便利实例
    TNetEncoding.Base64
    创建您自己的实例,并在
    create
    中使用0指定
    CharsPerLine
    参数

      encoding := TBase64Encoding.Create(0);
      try
        encosing.Encode(inStream, outStream);
      finally
        encoding.Free;
      end;
    

    代替便利实例
    TNetEncoding.Base64
    创建您自己的实例,并在
    create
    中使用0指定
    CharsPerLine
    参数

      encoding := TBase64Encoding.Create(0);
      try
        encosing.Encode(inStream, outStream);
      finally
        encoding.Free;
      end;
    

    在这个问题上有很多重复…..在这个问题上有很多重复。。。。。