悲催的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页珍藏版)》请在冰豆网上搜索。
![悲催的idhttpServer Get Post Upload 上传 multipart form post.docx](https://file1.bdocx.com/fileroot1/2023-1/21/e5aac506-566d-4634-b20e-9bfc391c455f/e5aac506-566d-4634-b20e-9bfc391c455f1.gif)
悲催的idhttpServerGetPostUpload上传multipartformpost
悲催的idhttpServerGetPostUpload上传multipartformpost
悲催的idhttpServer.Get浏览网页编码问题.明明网页中有chaset,但中文仍然显示为乱码.必须在ContentType中指定chaset,
AResponseInfo.CharSet设置无效?
?
?
contentType:
=GetMimeTypeFromFile(LocalDoc);
ifLeftStr(ContentType,4)='text'then
begin
ContentType:
=ContentType+';charset=gb2312';
end;
AResponseInfo.ContentType:
=contentType;AResponseInfo.ContentText指定为含有中文的字符串,会显示乱码.
用流.AResponseInfo.ResponseNo:
=404;
AResponseInfo.ContentType:
='text/plain'+';charset=gb2312';
//乱码须用流的形式
{memoryStream:
=TMemoryStream.Create;
s:
='找不到'+ARequestInfo.Document;
memorystream.write(PAnsichar(s)^,length(s));
AResponseInfo.ContentStream:
=memorystream;}
AResponseInfo.ContentText:
=
'<html>'+#13#10+
'<body>'+#13#10+
'Pagenotfound:
<br>'+#13#10+
'<b>'+ARequestInfo.Document+'</b>'+#13#10+
'</body>'+#13#10+
'</html>';对于存在的文件,用AResponseInfo.ServeFile(AContext,rPage)来试试看.可能可以避免乱码,未实验,估计可行?
procedureTfrmMain.ServerCommandGet(AContext:
TIdContext;
ARequestInfo:
TIdHTTPRequestInfo;AResponseInfo:
TIdHTTPResponseInfo);
var
rPage:
String;
begin
Log('Serving:
'+ARequestInfo.Document+'to'+AContext.Connection.Socket.Host,clBlue);
if(ARequestInfo.Document<>'')and
(ARequestInfo.Document<>'/')and
(ARequestInfo.Document<>'\')then
rPage:
=Copy(ARequestInfo.Document,2,Length(ARequestInfo.Document))
else
rPage:
='Index.htm';
rPage:
=StringReplace(rPage,'/','\',[rfReplaceAll,rfIgnoreCase]);
rPage:
=IncludeTrailingBackslash(edServerRoot.Text)+rPage;
ifFileExists(rPage)then
AResponseInfo.ServeFile(AContext,rPage)
else
AResponseInfo.ContentText:
='<H1>ERROR</H1>Filenotfound:
'+ARequestInfo.Document;
end;Post上传文件.
procedureTForm1.IdHTTPServerCreatePostStream(AContext:
TIdContext;
AHeaders:
TIdHeaderList;varVPostStream:
TStream);
begin
//VPostStream:
=TFileStream.Create('c:
\1111111',fmCreate);
//会包含所有信息,不仅仅上传的文件,还有http协议信息
VPostStream:
=TMemoryStream.Create;
end;procedureTForm1.IdHTTPServerCommandGet(AContext:
TIdContext;
ARequestInfo:
TIdHTTPRequestInfo;AResponseInfo:
TIdHTTPResponseInfo);
var
LocalDoc:
string;
ByteSent:
Cardinal;
ResultFile:
TFileStream;
memoryStream:
TMemoryStream;
contentType:
string;
s:
String;
Decoder,NewDecoder:
TIdMessageDecoder;
DestStream:
TStream;
IdPostStream,IdDestStream:
TStream;//TIdStreamVcl;
MsgEnd:
Boolean;
Stream:
TMemoryStream;
procedureDecodeFormData(constHeader:
string;ASourceStream:
TStream);
var
MsgEnd:
Boolean;
Decoder:
TIdMessageDecoder;
Tmp:
string;
Dest:
TMemoryStream;
begin
MsgEnd:
=False;
Decoder:
=TIdMessageDecoderMIME.Create(nil);
try
Decoder.SourceStream:
=ASourceStream;
Decoder.FreeSourceStream:
=False;
TIdMessageDecoderMIME(Decoder).MIMEBoundary:
=Decoder.ReadLn;
//Decoder.ReadLn;
repeat
Decoder.ReadHeader;
{caseDecoder.PartTypeof
mcptUnknown:
raiseException('Unknownformdatadetected');
mcptText:
begin
Tmp:
=Decoder.Headers.Values['Content-Type'];
Dest:
=TMemoryStream.Create;
try
Decoder:
=Decoder.ReadBody(Dest,MsgEnd);
ifAnsiSameText(Fetch(Tmp,';'),'multipart/mixed')then
DecodeFormData(Tmp,Dest)
else
//useDestasneeded...
finally
FreeAndNil(Dest);
end;
end;
mcptAttachment:
}
begin
Tmp:
=ExtractFileName(Decoder.FileName);
ifTmp<>''then
Tmp:
=ExtractFilePath(Application.ExeName)+'web\upload\'+Tmp
else
Tmp:
=MakeTempFilename('c:
\temp\');
Dest:
=TMemoryStream.Create;//TFileStream.Create(Tmp,fmCreate);
try
NewDecoder:
=Decoder.ReadBody(Dest,MsgEnd);
///////yuaddbegin
ifNewDecoder<>nilthen
begin
NewDecoder.SourceStream:
=ARequestInfo.PostStream;//yu
NewDecoder.FreeSourceStream:
=False;//yu
TIdMessageDecoderMIME(NewDecoder).MIMEBoundary:
=TIdMessageDecoderMIME(Decoder).MIMEBoundary;
end;
if(Decoder<>nil)and(Decoder.Filename<>'')then
Dest.SaveToFile(Tmp);
///////////yuaddend
Decoder.Free;
Decoder:
=NewDecoder;finally
FreeAndNil(Dest);
end;
end;
//end;
until(Decoder=nil)orMsgEnd;
finally
FreeAndNil(Decoder);
end;
end;
begin
LocalDoc:
=ExpandFileName(Edit1.Text+ARequestInfo.Document);
ifLocalDoc[Length(LocalDoc)]='\'then
Delete(LocalDoc,Length(LocalDoc),1);ifnotFileExists(LocalDoc)andDirectoryExists(LocalDoc)then
ifFileExists(ExpandFileName(LocalDoc+'/index.html'))then
LocalDoc:
=ExpandFileName(LocalDoc+'/index.html')
elseifFileExists(ExpandFileName(LocalDoc+'/index.htm'))then
LocalDoc:
=ExpandFileName(LocalDoc+'/index.htm');S:
=ARequestInfo.ContentType;
ifAnsiSameText(Fetch(S,';'),'multipart/form-data')then
begin
DecodeFormData(S,ARequestInfo.PostStream);end
else
//userequestdataasneeded...ifFileExists(LocalDoc)then
begin
ifAnsiSameText(Copy(LocalDoc,1,Length(edit1.text)),Edit1.Text)then
begin
ResultFile:
=TFileStream.create(LocalDoc,fmOpenReador
fmShareDenyWrite);
try
AResponseInfo.ResponseNo:
=200;contentType:
=GetMimeTypeFromFile(LocalDoc);
ifLeftStr(ContentType,4)='text'then
begin
ContentType:
=ContentType+';charset=gb2312';
end;AResponseInfo.ContentType:
=contentType;
AResponseInfo.ContentLength:
=ResultFile.Size;
AResponseInfo.ContentStream:
=ResultFile;
except
ResultFile.Free;
//Wemustfreethisfilesinceitwon'tbedonebythewebservercomponent
end;
//下载文件
//ByteSent:
=AResponseInfo.ServeFile(AContext,LocalDoc);
end;
end
else
begin
AResponseInfo.ResponseNo:
=404;
AResponseInfo.ContentType:
='text/plain'+';charset=gb2312';
//乱码须用流的形式
{memoryStream:
=TMemoryStream.Create;
s:
='找不到'+ARequestInfo.Document;
memorystream.write(PAnsichar(s)^,length(s));
AResponseInfo.ContentStream:
=memorystream;}
AResponseInfo.ContentText:
=
'<html>'+#13#10+
'<body>'+#13#10+
'Pagenotfound:
<br>'+#13#10+
'<b>'+ARequestInfo.Document+'</b>'+#13#10+
'</body>'+#13#10+
'</html>';end;
Memo1.Lines.Add(AReQuestInfo.Document);
end;TIdMessageDecoderMIME.ReadBody
跟踪发现
//不大明白为啥要多加'--'
ifMIMEBoundary<>''thenbegin
BoundaryStart:
='--'+MIMEBoundary;{DonotLocalize}
BoundaryEnd:
=BoundaryStart+'--';{DonotLocalize}
end;实际上传文件测试发现,发送的数据流中没有多加的'--'.多此一举?
?
ifIsBinaryContentTransferEncodingthen
begin
//Forbinary,needEOLbecausethedefaultLFcausesspuriousCRsintheoutput...
LLine:
=ReadLnRFC(VMsgEnd,EOL,'.',Indy8BitEncoding);//donotlocalize
end上传rar文件测试,发现rar文件中的单个的#10,#13字符,全部被替换成了#13#10两个字符,
结果文件变大了,rar文件也无法打开.
明明已经说了IsBinaryContentTransferEncoding,还要多此一举.
搞了一天,上传也没搞好.要不自己处理post收到的数据,不用
Decoder:
=Decoder.ReadBody(Dest,MsgEnd);idhhtp阻塞式.可设置为非阻塞式ICS非阻塞式使用ICSV8Gold.
编译WebDemos下的OverbyteIcsWebServ
启动OverbyteIcsWebServ.exe
访问http:
//localhost/formupload.html上传正常.