BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to user IdMessageDecoderMIME with HttpServer...

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock
View previous topic :: View next topic  
Author Message
Paulo Paredes
Guest





PostPosted: Tue Mar 29, 2005 3:22 pm    Post subject: how to user IdMessageDecoderMIME with HttpServer... Reply with quote



How can I decode onde file uploaded whit TidHttpServer?


I try this on post metod (but they show just one header, and that is not the
file uploaded, is the comand of the form):

--------------------------
SaveString('c:jabacule.txt', ARequestInfo.UnparsedParams);

fArquUpload := TMemoryStream.Create;
fArquUpload.LoadFromFile('c:jabacule.txt');

IdMessageDecoderMIME1.SourceStream := TidStreamVCL.Create(fArquUpload ,
False);
IdMessageDecoderMIME1.ReadHeader;
---------------------------


SaveString just save to file:

procedure SaveString(sArq, sContent : String);
var
F : TextFile;
begin
if FileExists(sArq) then
DeleteFile(sArq);

FileClose(FileCreate(sArq));

AssignFile(F, sArq);
Rewrite(F);
Write(F, sContent);
CloseFile(F);
end;


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Mar 29, 2005 9:31 pm    Post subject: Re: how to user IdMessageDecoderMIME with HttpServer... Reply with quote




"Paulo Paredes" <paulo (AT) endosoft (DOT) com.br> wrote


Quote:
I try this on post metod (but they show just one header, and
that is not the file uploaded, is the comand of the form):

First, get the file data from the ARequestInfo.PostStream property instead.

Second, this approach will only work if the form was sent using the
"multipart/form-data" ContentType, so you should be checking for that, if
you are not already.

Third, calling ReadHeader() is only part of the decoding process. You need
to also use ReadBody() to get to the actual data.

For example (untested):


procedure TForm1.IdHTTPServer1CreatePostStream(AContext TIdContext; var
VPostStream: TStream);
begin
VPostStream := TMemoryStream.Create;
end;

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
Decoder, Temp: TIdMessageDecoder;
MemStream: TStream;
IdPostStream, IdMemStream: TIdStreamVCL;
MsgEnd: Boolean;
Boundary: String;
begin
//...
if TextIsSame(ARequestInfo.ContentType, 'multipart/form-data') then
begin
ARequestInfo.PostStream.Position := 0;
IdPostStream := TIdStreamVCL.Create(ARequestInfo.PostStream);
try
MemStream := TMemoryStream.Create;
try
IdMemStream := TIdStreamVCL.Create(MemStream);
try
Boundary := IdPostStream.ReadLn;
Decoder := TIdMessageDecoderMIME.Create(nil);
try
repeat
Decoder.SourceStream := IdPostStream;
Decoder.MIMEBoundary := Boundary;
MemStream.Clear;
Decoder.ReadHeader;
Temp := Decoder.ReadBody(IdMemStream,
MsgEnd);
// process MemStream as needed...
FreeAndNil(Decoder);
Decoder := Temp;
until Decoder = nil or MsgEnd;
finally
FreeAndNil(Decoder);
end;
finally
FreeAndNil(IdMemStream);
end;
finally
FreeAndNil(MemStream);
end;
finally
FreeAndNil(IdPostStream);
end;
end;
//...
end;


Gambit



Back to top
Paulo Paredes
Guest





PostPosted: Thu Mar 31, 2005 4:43 pm    Post subject: Re: how to user IdMessageDecoderMIME with HttpServer... Reply with quote



One more time Remy solves the problem
I love you man....

heheheh...


"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> escreveu na mensagem
news:4249c8d1$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Paulo Paredes" <paulo (AT) endosoft (DOT) com.br> wrote in message
news:4249721d (AT) newsgroups (DOT) borland.com...

I try this on post metod (but they show just one header, and
that is not the file uploaded, is the comand of the form):

First, get the file data from the ARequestInfo.PostStream property
instead.

Second, this approach will only work if the form was sent using the
"multipart/form-data" ContentType, so you should be checking for that, if
you are not already.

Third, calling ReadHeader() is only part of the decoding process. You
need
to also use ReadBody() to get to the actual data.

For example (untested):


procedure TForm1.IdHTTPServer1CreatePostStream(AContext TIdContext;
var
VPostStream: TStream);
begin
VPostStream := TMemoryStream.Create;
end;

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
Decoder, Temp: TIdMessageDecoder;
MemStream: TStream;
IdPostStream, IdMemStream: TIdStreamVCL;
MsgEnd: Boolean;
Boundary: String;
begin
//...
if TextIsSame(ARequestInfo.ContentType, 'multipart/form-data')
then
begin
ARequestInfo.PostStream.Position := 0;
IdPostStream := TIdStreamVCL.Create(ARequestInfo.PostStream);
try
MemStream := TMemoryStream.Create;
try
IdMemStream := TIdStreamVCL.Create(MemStream);
try
Boundary := IdPostStream.ReadLn;
Decoder := TIdMessageDecoderMIME.Create(nil);
try
repeat
Decoder.SourceStream := IdPostStream;
Decoder.MIMEBoundary := Boundary;
MemStream.Clear;
Decoder.ReadHeader;
Temp := Decoder.ReadBody(IdMemStream,
MsgEnd);
// process MemStream as needed...
FreeAndNil(Decoder);
Decoder := Temp;
until Decoder = nil or MsgEnd;
finally
FreeAndNil(Decoder);
end;
finally
FreeAndNil(IdMemStream);
end;
finally
FreeAndNil(MemStream);
end;
finally
FreeAndNil(IdPostStream);
end;
end;
//...
end;


Gambit





Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.