mormot multipart上传文件
2021/7/14 6:34:53
本文主要是介绍mormot multipart上传文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
mormot multipart上传文件
首先修改mormot的MultiPartFormDataEncode()
function MultiPartFormDataEncode(const MultiPart: TMultiPartDynArray; var MultiPartContentType, MultiPartContent: RawUTF8): boolean; var len, boundcount, filescount, i: integer; boundaries: array of RawUTF8; bound: RawUTF8; W: TTextWriter; temp: TTextWriterStackBuffer; procedure NewBound; var random: array[1..3] of cardinal; begin FillRandom(@random,3); bound := BinToBase64(@random,SizeOf(Random)); SetLength(boundaries,boundcount+1); boundaries[boundcount] := bound; inc(boundcount); end; begin result := false; len := length(MultiPart); if len=0 then exit; boundcount := 0; filescount := 0; W := TTextWriter.CreateOwnedStream(temp); try // header - see https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html NewBound; //MultiPartContentType := 'Content-Type: multipart/form-data; boundary='+bound; MultiPartContentType := 'multipart/form-data; boundary='+bound; for i := 0 to len-1 do with MultiPart[i] do begin if FileName='' then W.Add('--%'#13#10'Content-Disposition: form-data; name="%"'#13#10+ 'Content-Type: %'#13#10#13#10'%'#10'--%'#13#10, [bound,Name,ContentType,Content,bound]) else begin // if this is the first file, create the header for files if filescount=0 then begin if i>0 then NewBound; W.Add('Content-Disposition: form-data; name="files"'#13#10+ 'Content-Type: multipart/mixed; boundary=%'#13#10#13#10,[bound]); end; inc(filescount); W.Add('--%'#13#10'Content-Disposition: form-data; name="%" filename="%"'#13#10+ 'Content-Type: %'#13#10,[bound, Name, FileName,MultiPartContentType]); // W.Add('--%'#13#10'Content-Disposition: file; filename="%"'#13#10+ // 'Content-Type: %'#13#10,[bound,FileName,ContentType]); if Encoding<>'' then W.Add('Content-Transfer-Encoding: %'#13#10,[Encoding]); W.AddCR; W.AddString(MultiPart[i].Content); W.Add(#13#10'--%'#13#10,[bound]); end; end; // footer multipart for i := boundcount-1 downto 0 do W.Add('--%--'#13#10, [boundaries[i]]); W.SetText(MultiPartContent); result := True; finally W.Free; end; end;
var part: SynCommons.TMultiPart; parts: SynCommons.TMultiPartDynArray; sFile, fileName : string; fs: Tfilestream; rs: TRawByteStringStream; data, ContentType : RawUTF8; begin //打开文件 if not OpenDialog1.Execute then Exit; sFile := OpenDialog1.FileName; fileName := AnsiToUtf8(ExtractFileName(sFile)); try fs := Tfilestream.Create(sFile, fmOpenRead); rs := TRawByteStringStream.Create; rs.CopyFrom(fs, fs.Size); SetLength(parts, 1); part.ContentType := 'multipart/form-data;boundary='; part.Name := 'xssjdb'; part.FileName := fileName;//文件名,上传至服务端的文件名,一般是原文件名,可以是别名 part.Content := rs.DataString; //文件内容 parts[0] := part; //添加参数, 服务端以request的query参数接收。与普通参数一样 syncommons.MultiPartFormDataAddField('filename', fileName, parts); SynCommons.MultiPartFormDataEncode(parts, ContentType, data); finally rs.Free; fs.Free; end; if http.Post('访问的地址url', data, ContentType) = 200 then begin //Memo1.Lines.Add(Utf8ToAnsi(http.Content)); end; end;
这篇关于mormot multipart上传文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16使用vue3+springboot构建简单Web应用教程
- 2024-11-15全栈开发项目实战:从入门到初级项目的实现
- 2024-11-15数据库项目实战:从入门到初级应用教程
- 2024-11-15IDEA项目实战入门教程
- 2024-11-15IT编程项目实战:新手入门的全面指南
- 2024-11-15Java开发项目实战:新手入门与初级技巧
- 2024-11-15Java零基础项目实战:从入门到独立开发
- 2024-11-15MyBatis Plus教程:入门与基础操作详解
- 2024-11-15MyBatis-Plus教程:新手入门与实战技巧
- 2024-11-15MyBatis教程:从入门到实践