How can I accept files via form to my Indy HTTP server?

How can I accept files via form to my Indy HTTP server?

Post by Reine » Tue, 21 Oct 2003 15:00:28


How can you decode the TIdHTTPRequestInfo when a form file (input
type="file") is used?

I have hunted high and low (on Google web and groups) on how to do this and
spent a few hours trying demo projects using things like
TIdMessageDecoderMIME (see code below) to no avail.

Basically the server gets the data and I can see it encoded in
TIdHTTPRequestInfo.UnparsedParams but I can't easily do something with it. I
have posted a sample form, and the data that I get when I upload a file, in
the attachments group.
"form upload files"


As mentioned above, I have tried decoding this using Indy tools, including
the following code (which incidentally gets into an infinite loop inside
Indy as it fails to handle the end of the stream properly). The following
creates a stream from a selected file.

--

procedure TForm1.Button3Click(Sender: TObject);
var
decoder, temp : TIdMessageDecoderMIME;
outfstream : TFileStream;
filename : string;
idx : integer;
isend : boolean;
begin
idx := 1;
decoder := TIdMessageDecoderMIME.Create(Self);
try
filename := OpenDialog.FileName;
{ open stream }
decoder.SourceStream := TFileStream.Create(filename, fmOpenRead or
fmShareDenyWrite);
decoder.MIMEBoundary := TIdStream(decoder.SourceStream).ReadLn;
decoder.SourceStream.Position := 0;

while Assigned(decoder) do
begin
filename := Copy(filename, 1, LastDelimiter('.', filename)-1) +
IntToStr(idx) + '.msg';
outfstream := TFileStream.Create(filename, fmCreate or
fmShareDenyWrite);
try
temp := TIdMessageDecoderMIME(decoder.ReadBody(outfstream, isend));
finally
FreeAndNil(decoder);
outfstream.Free;
end;
decoder := temp;
inc(idx);
end;
finally
if Assigned(decoder) then
decoder.Free;
end;
end;
 
 
 

How can I accept files via form to my Indy HTTP server?

Post by Reine » Fri, 31 Oct 2003 13:51:57

Never mind I guess since I wrote my own unit to decode it. Obviously hardly
anyone has the need to do this.

Is anyone actually running a real Indy web server or am I the only one?

 
 
 

How can I accept files via form to my Indy HTTP server?

Post by Martin Jam » Fri, 31 Oct 2003 14:10:49


hardly

There are at leaset two of us, though mine are all on intranets.

What is 'files via form'??

Rgds,
Martin
 
 
 

How can I accept files via form to my Indy HTTP server?

Post by Team » Fri, 31 Oct 2003 14:51:50


Sure, many people are (they are not necessary here in the groups, but they
are out there in the real world). But uploading files via HTTP is not
necessary a common practice. Although HTTP can upload files to a server,
the server has to be able to accept such transfers. FTP handles the scenrio
must better, and in fact most web servers do use FTP to upload files with
instead.


Gambit
 
 
 

How can I accept files via form to my Indy HTTP server?

Post by Reine » Fri, 31 Oct 2003 14:52:48


I'm running a public server with about 5 domains *** off it and also and
FTP server.


If you have a HTML form and put the following field type in:
<form action="cgi-bin/getfile" enctype="multipart/form-data" method="post">
<input type="file" name="afile">
</form>

What happens is the server receives the file in the RequestInfo but there's
nothing to decode it.
 
 
 

How can I accept files via form to my Indy HTTP server?

Post by Reine » Sat, 01 Nov 2003 09:13:15


scenrio

If you have a web site where you invite visitors to upload their own
pictures and comments, for example, you need a form to handle file uploads.
I wish to create this sort of thing on my server. Webmail software also
needs this sort of thing to handle attachments. FTP is not suitable for this
sort of thing. Indy does get the information/files in
RequestInfo.UnparsedParams but it can't be decoded (by any of the tools I
found). It took me half a day but I now have something that decodes it for
me.