mormot multipart上传文件
mormot multipart上传文件
首先修改mormot的MultiPartFormDataEncode()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 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; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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; |
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/15009171.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-07-14 异步任务调度二
2019-07-14 咏南中间件和开发框架全面支持DELPHI10.3.2
2019-07-14 DELPHI10.3.2安卓SDK安装
2016-07-14 匿名方法实现多线程同步到主线程执行
2016-07-14 DELPHI跨平台的临界替代者
2014-07-14 excel 的一些操作