本文整理自网络,侵删。
首先,上传文件的网页代码:
<form action="../testWebBroker.exe/upload" method="post" enctype="multipart/form-data"> <input type="file" id="MyUpload" name="MyUpload"> <input value="submit" type="submit"> </form>
上面的 action 里面的 "upload" 是指 WebBroker 里面的一个 Action,一个路径的名字。
注意:<input type="file" > 这样在服务器端是看不到文件的。必须要为这个元素加上 name="MyName" 才行!比如:<input type="file" name="file1">
然后,在 WebBroker 对应的路径底下就可以收到文件了。一开始收不到文件,是因为
1. 网页代码缺少了enctype="multipart/form-data" 这个描述。
2. 没有 uses WEB.ReqMulti;
以下是对应网页的服务器端 WebBroker 里面的代码:
FFileName := '文件名' + Request.Files[0].FileName; AFileName := ExtractFileName(Request.Files[0].FileName); AFileName := ExtractFilePath(GetModuleName(0)) + AFileName; AFile := TFileStream.Create(AFileName, fmCreate); try Request.Files[0].Stream.Position := 0; AFile.CopyFrom(Request.Files[0].Stream, Request.Files[0].Stream.Size); //测试保存文件,通过。 finally AFile.Free; end; FFileName := HTMLEncode(FFileName); PageProducerUpload.HTMLFile := 'FileUpload.htm'; //设计期不要指定 PageProducer 的 HTML 模板文件,而是在这里指定。如果在设计期指定,则 PageProducer 的 OnHTMLTag 事件会先于本路径事件的执行,则新页面无法显示上传的文件名。 Response.Content := PageProducerUpload.Content;
--------------------------
另:如果一个网页的 Form 里面有多个文件上传,则需要用到 WEB.ReqMulti 单元的 TMultipartContentParser。网上帮助说:
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/ReqMulti_TMultipartContentParser.html
Web request objects use TMultipartContentParser object to parse the content of an HTTP request message from a multipart form when that request may contain uploaded files. The Web request uses this content parser to assign values for its ContentFields and Files properties.
TMultipartContentParser is only used when the current HTTP request object has a content type of 'multipart/form-data'. Multipart forms built using a WebSnap application automatically use this content type.
To allow your WebSnap application to use TMultipartContentParser in Delphi, add the ReqMulti unit to the end of your project uses clause. In C++, include ReqMulti.hpp in the header of your project source file. ――――――――――――――――
原文链接:https://blog.csdn.net/pcplayer/article/details/69230492
相关阅读 >>
Delphi 10 下提示sharedactivitycontext错误的解决方法
Delphi google text to speech api
更多相关阅读请进入《Delphi》频道 >>