3

我有一个简单的 DataSnap REST 服务器,在 TWebModule 上我添加了一个动作来监听路径“/upload_file”上的 POST 请求。在 onAction 事件中,我有以下代码:

procedure TWebModule.WebModulePostFileAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var i: Integer;
  str: String;
begin
  for i :=0 to Request.Files.Count-1 do
  Begin
    Str:= Str +','+ Request.Files.Items[i].FileName;
    // I will like to save or move the uploaded file to i.e. c:\myFiles\
  End;
  Response.Content:= Str;
end;

Request.Files.Count 始终为 0,但是当我检查 Request.content 对象时,我可以看到多部分文件内容。如何从请求中获取发布的文件?并将其保存在磁盘中的某个位置。
我发布的 HTML 表单如下所示:

<form action="/upload_file" method="POST" enctype="multipart/form-data">
  Notes: <input type="text" name="notes"/>
  File: <input type="file" name="my_file"/>
  <input type="submit" value="Submit">
</form>

不幸的是,没有太多关于这个主题的信息,我已经尝试了几个小时但没有成功。提前致谢。

4

1 回答 1

0

See related question TIdHTTPServer file upload and answer https://stackoverflow.com/a/6197138/80901:

TIdHTTPServer does not currently support multipart/form-data submissions natively. That is on the todo list for Indy 11. In the meantime, you have to parse the posted MIME data manually using TIdDecoderMIME, as mjn suggested. There have been examples of that posted in the Embarcadero and Indy forums before.

于 2013-03-21T10:32:44.900 回答