Delphi WebBroker 上传文件


本文整理自网络,侵删。

 
首先,上传文件的网页代码:

      <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 将整数转换为罗马数字

Delphi打开网址链接的四种方法

Delphi驱动开发研究第一篇--实现原理

如何在Delphi中获取字符串字节数?

Delphi 使richedit中的链接可以点击

Delphi 使用indy idftp 通过android 上传文件

Delphi 检查当前用户权限

Delphi webbrowser通过遍历id查找元素是否存在

Delphi 取得某一天所在的星期一及星期天

Delphi中的split函数

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...