Indy(ver。10.5.5)TIdHTTPServerにファイルをアップロードしようとしています。
私は解決策を探していましたが、これまでのところ運がありません。私が見つけたのは、Delphi2010に同梱されているバージョンと互換性のない古いバージョンのIndyでした。
私が望んでいるのは、「multipart / form-data」を使用してファイルをサーバーにアップロードし、それをデコードするだけです。そのように簡単です。助けていただければ幸いです。
Indy(ver。10.5.5)TIdHTTPServerにファイルをアップロードしようとしています。
私は解決策を探していましたが、これまでのところ運がありません。私が見つけたのは、Delphi2010に同梱されているバージョンと互換性のない古いバージョンのIndyでした。
私が望んでいるのは、「multipart / form-data」を使用してファイルをサーバーにアップロードし、それをデコードするだけです。そのように簡単です。助けていただければ幸いです。
TIdHTTPServer
multipart/form-data
現在、ネイティブでの送信はサポートされていません。TIdDecoderMIME
これはIndy11のtodoリストにあります。それまでの間、mjnが提案したように、投稿されたMIMEデータを手動で解析する必要があります。以前にエンバカデロとインディのフォーラムに投稿された例があります。
Delphiを使用してWebサイトを構築する方法として、 xxmを開始し、変更が加えられた後、ブラウザーの更新ボタンを押すだけで、HTMLコードとPascalコードの両方を含むスクリプトを再コンパイルしました。
これは、IIS、Apache、Internet Explorer、FireFoxに「プラグイン」する汎用インターフェースを使用し、スタンドアロンのHTTPexeもあります。インターフェイスは、ファイルがアップロードされるときにパラメーターのIxxmParameterPostFileを公開します。
例については、デモ4アップロードを参照してください。
個人的にテストせずに見つけることができる唯一の適切な解決策(これがあなたのニーズに合った解決策につながらない場合はお知らせください。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;
これは根強い問題だと私には思えます。私はインターネット上であらゆる種類の提案を見てきましたが、MsMultipartparser.pasに匹敵するものはありません。残念ながら、Unicodeでは機能しません。
なぜ私よりも賢い人がMsMultipartparser.pasを書き直して、私たち全員に非常に多くのトラブルを救わないのですか?
インディ-動作しません。
IPWorks-ファイル名などを提供するようです。ファイル自体を取得する方法を理解できません。
Alcinoe-説明はもちろんのこと、インストール手順もありません。XE8またはDelphi10では機能しないようです。
私は他のいくつかを見ましたが、それらはすべて実用的ではありません。