Delphi TIdHTTPServer文件上载
我正在尝试将文件上载到Indy(版本10.5.5)TIdHTTPServer。Delphi TIdHTTPServer文件上载,delphi,delphi-2010,indy10,Delphi,Delphi 2010,Indy10,我正在尝试将文件上载到Indy(版本10.5.5)TIdHTTPServer。 我一直在寻找解决方案,但到目前为止运气不好,我发现Indy的旧版本与Delphi2010附带的版本不兼容 我所希望的是实现简单的上传一个使用“多部分/表单数据”的文件到服务器并解码它,尽管如此简单,任何帮助都非常感谢。无需亲自测试(如果这不能让您找到满足您需求的有效解决方案,请告诉我,我将启动XE并制作更具说服力的内容)TIdHTTPServer目前不支持本机提交的多部分/表单数据。这在Indy 11的待办事项列表中
我一直在寻找解决方案,但到目前为止运气不好,我发现Indy的旧版本与Delphi2010附带的版本不兼容
我所希望的是实现简单的上传一个使用“多部分/表单数据”的文件到服务器并解码它,尽管如此简单,任何帮助都非常感谢。无需亲自测试(如果这不能让您找到满足您需求的有效解决方案,请告诉我,我将启动XE并制作更具说服力的内容)
TIdHTTPServer
目前不支持本机提交的多部分/表单数据。这在Indy 11的待办事项列表中。同时,您必须按照mjn的建议,使用TidDecoderMie
手动解析发布的MIME数据。Embarcadero和Indy论坛上已经发布了此类示例以前。我开始使用Delphi构建网站,在进行更改后,按一下浏览器的刷新按钮,重新编译包含HTML和Pascal代码的脚本
它使用一个通用接口“插入”IIS、Apache、Internet Explorer、FireFox,还有一个独立的HTTP exe。该接口在上传文件时在参数上公开IxxmParameterPostFile
请参阅以获取示例。
我执行一个简单的多部分上传,如下所示:
旧单位:
interface
uses
System.SysUtils, System.Classes, Web.Win.Sockets, IdBaseComponent,
IdComponent, IdCustomTCPServer, IdCustomHTTPServer, IdHTTPServer, IdContext,
IdTCPServer,System.Generics.Collections, Data.DB, Datasnap.DBClient,IdHeaderList,idGlobal,
IdIntercept,IdMessage,IdMessageCoderMIME,IdMessageCoder,IdGlobalProtocols;
在mime内容上循环的代码:
procedure TdmNet.IdHTTPServerCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
Var
ms : TMemoryStream;
newdecoder, Decoder: TIdMessageDecoder;
boundary, startboundary : String;
msgEnd : Boolean;
tmp : String;
I : Integer;
fname : String;
tsValues : TStringList;
begin
i := 0;
if pos('upload',lowercase(ARequestInfo.Document)) > 0 then
begin
If ARequestInfo.PostStream = nil then
AResponseInfo.ContentText := '<HTML><BODY>unparsed:' + ARequestInfo.UnparsedParams +
'<br>Encoding:' + ARequestInfo.ContentEncoding + ARequestInfo.RawHeaders.Values['Content-Type'] +
'<br>HashCode:' + IntToStr(ARequestInfo.GetHashCode) +
'<br>Params:' + ARequestInfo.Params.Text + ' -->stream nullo<br></BODY><HTML>'
Else
ARequestInfo.PostStream.Position := 0;
msgEnd := False;
boundary := ExtractHeaderSubItem(ARequestInfo.ContentType, 'boundary',QuoteHTTP);
startboundary := '--' + boundary;
repeat
tmp := ReadLnFromStream(ARequestInfo.PostStream, -1, True);
until tmp = startboundary;
decoder := TIdMessageDecoderMIME.Create(nil);
TIdMessageDecoderMIME(decoder).MIMEBoundary := boundary;
tsValues := TStringList.Create;
try
repeat
decoder.SourceStream := ARequestInfo.PostStream;
decoder.FreeSourceStream := false;
decoder.ReadHeader;
inc(I);
case Decoder.PartType of
mcptAttachment,mcptText : begin
ms := TMemoryStream.Create;
ms.Position := 0;
newdecoder := Decoder.ReadBody(ms,msgEnd);
tmp := Decoder.Headers.Text;
fname := decoder.Filename;
decoder.Free;
decoder := newdecoder;
if decoder <> nil then
TIdMessageDecoderMIME(decoder).MIMEBoundary := boundary;
sleep(100);
if fname <> '' then
begin
ms.SaveToFile(fname);
//msgEnd := true;
end
else
begin
ms.SaveToFile(inttostr(i) + '.txt');
end;
ms.Free;
end;
mcptIgnore: Begin
try
FreeAndNil(decoder);
decoder := TIdMessageDecoderMIME.Create(nil);
TIdMessageDecoderMIME(decoder).MIMEBoundary := boundary;
finally
ms.Free;
end;
End;
mcptEOF: begin FreeAndNil(decoder); msgEnd := True end;
end;
until (decoder = nil) or(msgEnd);
finally
if decoder <> nil then
decoder.Free;
end;
AResponseInfo.ContentText := AResponseInfo.ContentText + '</BODY><HTML>';
AResponseInfo.ContentText := '<HTML><BODY>unparsed:' + ARequestInfo.UnparsedParams +
'<br>Encoding:' + ARequestInfo.ContentEncoding + '<br>Conte' + ARequestInfo.RawHeaders.Values['Content-Type'] +
'<br>HashCode:' + IntToStr(ARequestInfo.GetHashCode) +
'<br>Params:' + ARequestInfo.Params.Text + ' -->stream nullo<br></BODY><HTML>';
end
Else
Begin
AResponseInfo.ContentText :=
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ' + #10#13 +
'<html xmlns="http://www.w3.org/1999/xhtml"> ' + #10#13 +
'<head> ' + #10#13 +
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ' + #10#13 +
'<title>Pagina di prova</title> ' + #10#13 +
'</head> ' + #10#13 +
' ' + #10#13 +
'<body> ' + #10#13 +
'<h1>Test multipart from <a href="www.synaptica.info">Synaptica Srl</a> </h1> <BR><BR> ' + #10#13 +
'<form action="upload" method="post" enctype="multipart/form-data"> ' + #10#13 +
'<p>key ' + #10#13 +
' <input type="text" name="key" id="key" /> ' + #10#13 +
'</p> ' + #10#13 +
'<p>group ' + #10#13 +
' <input type="text" name="group" id="key" /> ' + #10#13 +
'</p> ' + #10#13 +
' ' + #10#13 +
'<label for="file">Filename:</label> ' + #10#13 +
'<label for="file">' + ARequestInfo.Document + '</label> ' + #10#13 +
'<input type="file" name="file" id="file" /> ' + #10#13 +
'<br /> ' + #10#13 +
'<input type="submit" name="submit" value="Submit" /> ' + #10#13 +
'</form></p> ' + #10#13 +
'</body> ' + #10#13 +
'</html> ';
End;
过程TdmNet.IdHTTPServerCommandGet(AContext:TIdContext;
ARequestInfo:TIdHTTPRequestInfo;AResponseInfo:TIdHTTPResponseInfo);
变量
ms:TMemoryStream;
新解码器,解码器:TIdMessageDecoder;
边界,起始边界:字符串;
msgEnd:布尔型;
tmp:字符串;
I:整数;
fname:字符串;
tsValues:TStringList;
开始
i:=0;
如果pos('upload',小写(ARequestInfo.Document))>0,则
开始
如果ARequestInfo.PostStream=nil,则
AresOnSeInfo.ContentText:='unparsed:'+ARequestInfo.UnparsedParams+
“
编码:”+ARequestInfo.ContentEncoding+ARequestInfo.RawHeaders.Values['Content-Type']+
“
HashCode:”+IntToStr(ARequestInfo.GetHashCode)+
“
参数:”+ARequestInfo.Params.Text+”-->stream nullo
'
其他的
ARequestInfo.PostStream.Position:=0;
msgEnd:=假;
边界:=ExtractHeaderSubItem(ARequestInfo.ContentType,'boundary',QuoteHTTP);
起始边界:='-'+边界;
重复
tmp:=ReadLnFromStream(ARequestInfo.PostStream,-1,True);
直到tmp=起始边界;
解码器:=TIdMessageDecoderMIME.Create(nil);
TIdMessageDecoderMIME(解码器).MIMEBoundary:=边界;
tsValues:=TStringList.Create;
尝试
重复
decoder.SourceStream:=ARequestInfo.PostStream;
decoder.FreeSourceStream:=假;
解码器.ReadHeader;
公司(一);
case Decoder.PartType of
MCPTETEXT:开始
ms:=TMemoryStream.Create;
女士职位:=0;
newdecoder:=解码器.ReadBody(ms,msgEnd);
tmp:=Decoder.Headers.Text;
fname:=解码器.Filename;
解码器。免费;
解码器:=新解码器;
如果解码器为零,则
TIdMessageDecoderMIME(解码器).MIMEBoundary:=边界;
睡眠(100);
如果fname为“”,则
开始
SaveToFile女士(fname);
//msgEnd:=真;
结束
其他的
开始
ms.SaveToFile(inttostr(i)+'.txt');
结束;
弗里女士;
结束;
麦克蒂诺尔:开始
尝试
FreeAndNil(译码器);
解码器:=TIdMessageDecoderMIME.Create(nil);
TIdMessageDecoderMIME(解码器).MIMEBoundary:=边界;
最后
弗里女士;
结束;
结束;
mcptEOF:begin FreeAndNil(解码器);msgEnd:=真结束;
结束;
直到(解码器=nil)或(msgEnd);
最后
如果解码器为零,则
解码器。免费;
结束;
AResponseInfo.ContentText:=AResponseInfo.ContentText+“”;
AresOnSeInfo.ContentText:='unparsed:'+ARequestInfo.UnparsedParams+
“
编码:”+ARequestInfo.ContentEncoding+”
Content'+ARequestInfo.RawHeaders.Values['Content-Type']+
“
HashCode:”+IntToStr(ARequestInfo.GetHashCode)+
“
参数:”+ARequestInfo.Params.Text+”-->stream nullo
”;
结束
其他的
开始
AResponseInfo.ContentText:=
' ' + #10#13 +
' ' + #10#13 +
' ' + #10#13 +
' ' + #10#13 +
“帕吉娜·迪普罗瓦”+#10#13+
' ' + #10#13 +
' ' + #10#13 +
'