HandleMultipartUpload(Request: TIdHTTPRequestInfo;Response: TIdHTTPResponseInfo);
var
LBoundary, LBoundaryStart, LBoundaryEnd:
string
;
LDecoder: TIdMessageDecoder;
LLine:
string
;
LBoundaryFound, LIsStartBoundary, LMsgEnd:
Boolean
;
Decoder: TIdMessageDecoderMIME;
LStream: TStream;
fstream:TmemoryStream;
I:
integer
;
begin
LBoundary := ExtractHeaderSubItem(Request
.
ContentType,
'boundary'
, QuoteHTTP);
if
LBoundary =
''
then
begin
Response
.
ResponseNo :=
400
;
Response
.
CloseConnection :=
true
;
Response
.
WriteHeader;
Exit;
end
;
LBoundaryStart :=
'--'
+ LBoundary;
LBoundaryEnd := LBoundaryStart +
'--'
;
LDecoder := TIdMessageDecoderMIME
.
Create(
nil
);
try
TIdMessageDecoderMIME(LDecoder).MIMEBoundary := LBoundary;
LDecoder
.
SourceStream := Request
.
PostStream;
LDecoder
.
FreeSourceStream :=
False
;
LBoundaryFound :=
False
;
LIsStartBoundary :=
False
;
repeat
LLine := ReadLnFromStream(Request
.
PostStream, -
1
,
true
);
if
LLine = LBoundaryStart
then
begin
LBoundaryFound :=
true
;
LIsStartBoundary :=
true
;
end
else
if
LLine = LBoundaryEnd
then
begin
LBoundaryFound :=
true
;
end
;
until
LBoundaryFound;
if
(
not
LBoundaryFound)
or
(
not
LIsStartBoundary)
then
begin
Response
.
ResponseNo :=
400
;
Response
.
CloseConnection :=
true
;
Response
.
WriteHeader;
Exit;
end
;
LMsgEnd :=
False
;
repeat
TIdMessageDecoderMIME(LDecoder).MIMEBoundary := LBoundary;
LDecoder
.
SourceStream := Request
.
PostStream;
LDecoder
.
FreeSourceStream :=
False
;
LDecoder
.
ReadHeader;
case
LDecoder
.
PartType
of
mcptText:
ProcessMimePart(LDecoder, LMsgEnd, Response);
mcptAttachment:
begin
fstream:=TmemoryStream
.
Create();
var
newDecor:TIdMessageDecoder:= LDecoder
.
ReadBody(fstream,LMsgEnd);
fstream
.
SaveToFile(GetUploadFolder+LDecoder
.
Filename) ;
LDecoder:=TIdMessageDecoderMime(newDecor);
continue;
end
;
mcptIgnore:
begin
LDecoder
.
Free;
LDecoder := TIdMessageDecoderMIME
.
Create(
nil
);
end
;
mcptEOF:
begin
LDecoder
.
Free;
LMsgEnd :=
true
;
end
;
end
;
until
(LDecoder =
nil
)
or
LMsgEnd;
finally
LDecoder
.
Free;
end
;
end
;