mormot上传文件

 

//服务器代码

(AContext: THttpServerRequest)

if Pos('multipart/form-data', AContext.InContentType) > 0 then
begin
  upload(AContext.InContentType, AContext.InContent); 
end;

//从请求中获取文件,保存到本地

function Upload(mimetype, params: RawUTF8): Boolean;
var
parts: TMultiPartDynArray;
i: Integer;
filename: string;
ss: TStringStream;
begin
Result := False;
try
MultiPartFormDataDecode(mimetype, params, parts);
if Length(parts) =0 then
begin
OutValue := '无文件!请检查!';
Exit;
end;
for i := 0 to high(parts) do
begin
if sametext(parts[i].Name, 'file') then
begin
ss := TStringStream.Create(parts[i].Content);
try
ss.SaveToFile(FPATH + '\' + parts[i].filename)
finally
ss.Free;
end;
end
else
begin
OutValue := '上传文件格式错误!请指定文件参数key=file!';
Exit
end;
end;
except
on e: Exception do
begin
OutValue := '文件保存出错!' + e.Message;
Exit
end;
end;
OutValue := '成功';
Result := True;
end;

客户端上传使用multipart/form-data格式,贴一个postman的上传示例,key一定要指定file:

 

posted @   Yang杨。  阅读(153)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示