在使用Lazarus向Deepseek投喂数据时,要上传文件,上传英文文件名没问题。上传中文文件名时AnythingLLM也能正常处理,但是文件名是乱码。在管理投喂文件时非常不方便。
var
HTTPClient: TFPHTTPClient;
Response: TStringStream;
//这行上传文件,服务器接收到中文件名乱码
HTTPClient.FileFormPost('http://myserver.com','files',filename,Response);
解决方法----------------------------------------------------------------
1、打开fphttpclient源
2、找到
procedure TFPCustomHTTPClient.StreamFormPost(const AURL: string;
FormData: TStrings; const AFieldName, AFileName: string;
const AStream: TStream; const Response: TStream);
Var
S, Sep : string;
SS : TRawByteStringStream;
I: Integer;
N,V: String;
Procedure WriteStringToStream (aString : String);
var
B : TBytes;
begin
{$IF SIZEOF(CHAR)=1}
B:=TEncoding.Default.GetAnsiBytes(aString);
{$ELSE}
B:=TEncoding.Default.GetBytes(aString);
{$ENDIF}
SS.WriteBuffer(B[0],Length(B));
end;
begin
Sep:=Format('%.8x_multipart_boundary',[Random($ffffff)]);
AddHeader('Content-Type','multipart/form-data; boundary='+Sep);
SS:=TRawByteStringStream.Create();
try
if (FormData<>Nil) then
for I:=0 to FormData.Count -1 do
begin
// not url encoded
FormData.GetNameValue(I,N,V);
S :='--'+Sep+CRLF;
S:=S+Format('Content-Disposition: form-data; name="%s"'+CRLF+CRLF+'%s'+CRLF,[N, V]);
WriteStringToStream(S);
end;
S:='--'+Sep+CRLF;
//s:=s+Format('Content-Disposition: form-data; name="%s"; filename="%s"'+CRLF,[AFieldName,ExtractFileName(AFileName)]);//这行不用,加下面一行
s:=s+Format('Content-Disposition: form-data; name="%s"; filename="%s";filename*=UTF-8''''%s'+CRLF,[AFieldName,ExtractFileName(AFileName),HTTPEncode(ExtractFileName(AFileName))]); //改成这行,要加httpprotocol引用
s:=s+'Content-Type: application/octet-string'+CRLF+CRLF;
WriteStringToStream(S);
AStream.Seek(0, soFromBeginning);
SS.CopyFrom(AStream,AStream.Size);
S:=CRLF+'--'+Sep+'--'+CRLF;
WriteStringToStream(S);
SS.Position:=0;
RequestBody:=SS;
Post(AURL,Response);
finally
RequestBody:=Nil;
SS.Free;
end;
end;
浙公网安备 33010602011771号