悲催的idhttpServer Get Post Upload 上传 multipart form post文档格式.docx
- 文档编号:20259232
- 上传时间:2023-01-21
- 格式:DOCX
- 页数:7
- 大小:16.83KB
悲催的idhttpServer Get Post Upload 上传 multipart form post文档格式.docx
《悲催的idhttpServer Get Post Upload 上传 multipart form post文档格式.docx》由会员分享,可在线阅读,更多相关《悲催的idhttpServer Get Post Upload 上传 multipart form post文档格式.docx(7页珍藏版)》请在冰豆网上搜索。
=TMemoryStream.Create;
s:
='
找不到'
+ARequestInfo.Document;
memorystream.write(PAnsichar(s)^,length(s));
AResponseInfo.ContentStream:
=memorystream;
}
AResponseInfo.ContentText:
=
'
&
lt;
html&
gt;
'
+#13#10+
body&
Pagenotfound:
br&
b&
+ARequestInfo.Document+'
/b&
/body&
/html&
对于存在的文件,用AResponseInfo.ServeFile(AContext,rPage)来试试看.可能可以避免乱码,未实验,估计可行?
procedureTfrmMain.ServerCommandGet(AContext:
TIdContext;
ARequestInfo:
TIdHTTPRequestInfo;
AResponseInfo:
TIdHTTPResponseInfo);
var
rPage:
String;
begin
Log('
Serving:
to'
+AContext.Connection.Socket.Host,clBlue);
if(ARequestInfo.Document&
)and
(ARequestInfo.Document&
/'
\'
)then
=Copy(ARequestInfo.Document,2,Length(ARequestInfo.Document))
else
Index.htm'
=StringReplace(rPage,'
'
[rfReplaceAll,rfIgnoreCase]);
=IncludeTrailingBackslash(edServerRoot.Text)+rPage;
ifFileExists(rPage)then
AResponseInfo.ServeFile(AContext,rPage)
H1&
ERROR&
/H1&
Filenotfound:
+ARequestInfo.Document;
end;
Post上传文件.
procedureTForm1.IdHTTPServerCreatePostStream(AContext:
AHeaders:
TIdHeaderList;
varVPostStream:
TStream);
//VPostStream:
=TFileStream.Create('
c:
\1111111'
fmCreate);
//会包含所有信息,不仅仅上传的文件,还有http协议信息
VPostStream:
=TMemoryStream.Create;
procedureTForm1.IdHTTPServerCommandGet(AContext:
LocalDoc:
string;
ByteSent:
Cardinal;
ResultFile:
TFileStream;
memoryStream:
TMemoryStream;
contentType:
Decoder,NewDecoder:
TIdMessageDecoder;
DestStream:
TStream;
IdPostStream,IdDestStream:
//TIdStreamVcl;
MsgEnd:
Boolean;
Stream:
procedureDecodeFormData(constHeader:
ASourceStream:
var
Decoder:
Tmp:
Dest:
MsgEnd:
=False;
Decoder:
=TIdMessageDecoderMIME.Create(nil);
try
Decoder.SourceStream:
=ASourceStream;
Decoder.FreeSourceStream:
TIdMessageDecoderMIME(Decoder).MIMEBoundary:
=Decoder.ReadLn;
//Decoder.ReadLn;
repeat
Decoder.ReadHeader;
{caseDecoder.PartTypeof
mcptUnknown:
raiseException('
Unknownformdatadetected'
);
mcptText:
Tmp:
=Decoder.Headers.Values['
Content-Type'
];
Dest:
=Decoder.ReadBody(Dest,MsgEnd);
ifAnsiSameText(Fetch(Tmp,'
),'
multipart/mixed'
DecodeFormData(Tmp,Dest)
//useDestasneeded...
finally
FreeAndNil(Dest);
mcptAttachment:
=ExtractFileName(Decoder.FileName);
ifTmp&
=ExtractFilePath(Application.ExeName)+'
web\upload\'
+Tmp
=MakeTempFilename('
\temp\'
//TFileStream.Create(Tmp,fmCreate);
NewDecoder:
///////yuaddbegin
ifNewDecoder&
nilthen
NewDecoder.SourceStream:
=ARequestInfo.PostStream;
//yu
NewDecoder.FreeSourceStream:
TIdMessageDecoderMIME(NewDecoder).MIMEBoundary:
=TIdMessageDecoderMIME(Decoder).MIMEBoundary;
if(Decoder&
nil)and(Decoder.Filename&
Dest.SaveToFile(Tmp);
///////////yuaddend
Decoder.Free;
=NewDecoder;
//end;
until(Decoder=nil)orMsgEnd;
FreeAndNil(Decoder);
LocalDoc:
=ExpandFileName(Edit1.Text+ARequestInfo.Document);
ifLocalDoc[Length(LocalDoc)]='
Delete(LocalDoc,Length(LocalDoc),1);
ifnotFileExists(LocalDoc)andDirectoryExists(LocalDoc)then
ifFileExists(ExpandFileName(LocalDoc+'
/index.html'
))then
=ExpandFileName(LocalDoc+'
)
elseifFileExists(ExpandFileName(LocalDoc+'
/index.htm'
S:
=ARequestInfo.ContentType;
ifAnsiSameText(Fetch(S,'
multipart/form-data'
DecodeFormData(S,ARequestInfo.PostStream);
end
//userequestdataasneeded...ifFileExists(LocalDoc)then
ifAnsiSameText(Copy(LocalDoc,1,Length(edit1.text)),Edit1.Text)then
ResultFile:
=TFileStream.create(LocalDoc,fmOpenReador
fmShareDenyWrite);
AResponseInfo.ResponseNo:
=200;
AResponseInfo.ContentLength:
=ResultFile.Size;
=ResultFile;
except
ResultFile.Free;
//Wemustfreethisfilesinceitwon'
tbedonebythewebservercomponent
//下载文件
//ByteSent:
=AResponseInfo.ServeFile(AContext,LocalDoc);
Memo1.Lines.Add(AReQuestInfo.Document);
TIdMessageDecoderMIME.ReadBody
跟踪发现
//不大明白为啥要多加'
--'
ifMIMEBoundary&
thenbegin
BoundaryStart:
+MIMEBoundary;
{DonotLocalize}
BoundaryEnd:
=BoundaryStart+'
实际上传文件测试发现,发送的数据流中没有多加的'
.多此一举?
ifIsBinaryContentTransferEncodingthen
//Forbinary,needEOLbecausethedefaultLFcausesspuriousCRsintheoutput...
LLine:
=ReadLnRFC(VMsgEnd,EOL,'
.'
Indy8BitEncoding);
//donotlocalize
end上传rar文件测试,发现rar文件中的单个的#10,#13字符,全部被替换成了#13#10两个字符,
结果文件变大了,rar文件也无法打开.
明明已经说了IsBinaryContentTransferEncoding,还要多此一举.
搞了一天,上传也没搞好.要不自己处理post收到的数据,不用
Decoder:
idhhtp阻塞式.可设置为非阻塞式ICS非阻塞式使用ICSV8Gold.
编译WebDemos下的OverbyteIcsWebServ
启动OverbyteIcsWebServ.exe
访问http:
//localhost/formupload.html上传正常.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 悲催的idhttpServer Get Post Upload 上传 multipart form idhttpServer
链接地址:https://www.bdocx.com/doc/20259232.html