我正在尝试将文件上传到 Indy(10.5.5 版)TIdHTTPServer。
我一直在寻找解决方案,但到目前为止没有运气,我发现旧版本的 Indy 与 Delphi 2010 附带的版本不兼容。
我希望实现简单地使用“multipart/form-data”将文件上传到服务器并对其进行解码,就这么简单,感谢您的帮助。
我正在尝试将文件上传到 Indy(10.5.5 版)TIdHTTPServer。
我一直在寻找解决方案,但到目前为止没有运气,我发现旧版本的 Indy 与 Delphi 2010 附带的版本不兼容。
我希望实现简单地使用“multipart/form-data”将文件上传到服务器并对其进行解码,就这么简单,感谢您的帮助。
TIdHTTPServer
目前不支持multipart/form-data
原生提交。这是 Indy 11 的待办事项列表。与此同时,您必须TIdDecoderMIME
按照 mjn 的建议使用 手动解析发布的 MIME 数据。之前在 Embarcadero 和 Indy 论坛上已经发布过这样的例子。
只有在没有亲自测试的情况下我才能找到合适的解决方案(如果这不能让您找到满足您需求的可行解决方案,请告诉我,我会启动 XE 并产生更有说服力的东西)
http://www.synaptica.info/2014/01/31/tidhttpserver-decode-content-type-multipartform-data/
我做了一个简单的分段上传,如下所示:
使用单位:
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;
It seems to me that this is a perennial problem. I have looked at all sorts of proposals on the internet and there is nothing comparable to MsMultipartparser.pas on offer. Sadly, it does not work for Unicode.
Why does not someone a lot smarter than me rewrite MsMultipartparser.pas and save us all an awful lot of trouble?
Indy - does not work.
IPWorks - seems to just give you filename(s) and so on. Can't work out how to get file itself.
Alcinoe - no installation instructions let alone explanation and does not seem to work for XE8 or Delphi 10
I looked at several others but they are all impractical.