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. 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值