Delphi中如何使用TIdMultiPartFormDataStream配合TIdHTTPServer实现文件上传?
- 内容介绍
- 文章标签
- 相关推荐
本文共计477个文字,预计阅读时间需要2分钟。
您好,我需要了解如何使用 Indy 的 IdHttpServer 检索参数和数据的相关帮助。我的应用程序通过 TIdMultiPartFormDataStream 通过 PHP 发送数据。我想使用 TIdHTTPServer 来验证某些参数并将请求转发到目标地址。
您好我需要有关如何使用indy的IdHttpServer检索参数和数据的帮助.我的许多应用程序使用TIdMultiPartFormDataStream通过php发送数据.我想使用TIdHTTPServer出于某种原因验证参数并将请求转发到目的地.
我为你创造了一个简短的例子.
uses IdContext, IdMultipartFormData; // Server Side------------------------------------------------ IdHTTPServer1.Defaultport := 88; IdHTTPServer1.active := True; procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); begin // the request will be pass through its destination by POST/GET // and send the result back to the client apps. AResponseInfo.ContentText := ARequestInfo.Params.Text; end; // Client Side------------------------------------------------ // This will work using the standard Post or Get procedure TForm1.btnPost1Click(Sender: TObject); var sl: TStringList; res: String; begin sl := TStringList.Create; try sl.Add('Param1=Data1'); sl.Add('Param2=Data1'); sl.Add('Param3=Data2'); sl.Add('Param4=Data3'); res := IdHTTP1.Post('localhost:88/some.php', sl); ShowMessage(res); finally sl.Free; end; end; //how can i get the parameters and value for this code in my IdHttpServer procedure TForm1.btnPost2Click(Sender: TObject); var mfd: TIdMultiPartFormDataStream; res: String; begin mfd := TIdMultiPartFormDataStream.Create; try mfd.AddFormField('Param1', 'Data1'); mfd.AddFormField('Param2', 'Data1'); mfd.AddFormField('Param3', 'Data2'); mfd.AddFormField('Param4', 'Data3'); res := IdHTTP1.Post('localhost:88/some.php', mfd); ShowMessage(res); finally mfd.Free; end; end;
以及我如何知道客户端应用程序是否通过TIdMultiPartFormDataStream类型的参数?
简而言之,当触发TIdHTTPServer.OnCommandGet事件时,如果AResponseInfo.ContentType属性表示multipart / form-data(您正在使用的TIdHTTP.Post()版本将发送application / x-www-form-urlencoded) ,AResponseInfo.PostStream属性将包含客户端发布的原始MIME数据.您可以使用TIdMessageDecoderMIME类来解析它.但是,该类从未打算在服务器端使用,因此使用起来不是很直观,但它仍然是可能的.
在Indy 11中,我计划直接将本机多部分/表单数据解析实现到TIdHTTPServer本身,但由于我们还没有开始使用Indy 11,因此还没有ETA.
本文共计477个文字,预计阅读时间需要2分钟。
您好,我需要了解如何使用 Indy 的 IdHttpServer 检索参数和数据的相关帮助。我的应用程序通过 TIdMultiPartFormDataStream 通过 PHP 发送数据。我想使用 TIdHTTPServer 来验证某些参数并将请求转发到目标地址。
您好我需要有关如何使用indy的IdHttpServer检索参数和数据的帮助.我的许多应用程序使用TIdMultiPartFormDataStream通过php发送数据.我想使用TIdHTTPServer出于某种原因验证参数并将请求转发到目的地.
我为你创造了一个简短的例子.
uses IdContext, IdMultipartFormData; // Server Side------------------------------------------------ IdHTTPServer1.Defaultport := 88; IdHTTPServer1.active := True; procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); begin // the request will be pass through its destination by POST/GET // and send the result back to the client apps. AResponseInfo.ContentText := ARequestInfo.Params.Text; end; // Client Side------------------------------------------------ // This will work using the standard Post or Get procedure TForm1.btnPost1Click(Sender: TObject); var sl: TStringList; res: String; begin sl := TStringList.Create; try sl.Add('Param1=Data1'); sl.Add('Param2=Data1'); sl.Add('Param3=Data2'); sl.Add('Param4=Data3'); res := IdHTTP1.Post('localhost:88/some.php', sl); ShowMessage(res); finally sl.Free; end; end; //how can i get the parameters and value for this code in my IdHttpServer procedure TForm1.btnPost2Click(Sender: TObject); var mfd: TIdMultiPartFormDataStream; res: String; begin mfd := TIdMultiPartFormDataStream.Create; try mfd.AddFormField('Param1', 'Data1'); mfd.AddFormField('Param2', 'Data1'); mfd.AddFormField('Param3', 'Data2'); mfd.AddFormField('Param4', 'Data3'); res := IdHTTP1.Post('localhost:88/some.php', mfd); ShowMessage(res); finally mfd.Free; end; end;
以及我如何知道客户端应用程序是否通过TIdMultiPartFormDataStream类型的参数?
简而言之,当触发TIdHTTPServer.OnCommandGet事件时,如果AResponseInfo.ContentType属性表示multipart / form-data(您正在使用的TIdHTTP.Post()版本将发送application / x-www-form-urlencoded) ,AResponseInfo.PostStream属性将包含客户端发布的原始MIME数据.您可以使用TIdMessageDecoderMIME类来解析它.但是,该类从未打算在服务器端使用,因此使用起来不是很直观,但它仍然是可能的.
在Indy 11中,我计划直接将本机多部分/表单数据解析实现到TIdHTTPServer本身,但由于我们还没有开始使用Indy 11,因此还没有ETA.

