[fpc-pascal] CGI upload app

Lukas Gradl fpc at ssn.at
Tue Nov 4 12:51:31 CET 2008


Graeme Geldenhuys schrieb:
> Hi Darius,
> 
> Did you ever manage to get an example of uploading a file?  I've got a
> CGI app based on Powtils or PSP or whatever it's called these days. My
> CGI app needs to permit uploading of files, but I got no idea how to
> do it.
> 
> Regards,
>   - Graeme -

You just want to upload a file to a server using a CGI running on that 
server?
I did this with cgiapp contained in the fpc-web package.

It's quite simple - if needed I could extract the relevant lines out of 
my (quite complex) CGI tomorrow and create a compilable example.

The relevant parts are:

uses cgiapp,...;

....

Type
   TYourNewCGI = Class(TCGIApplication)
     Procedure DoRun; override;
   end;

.....

procedure TYourNewCGI.DoRun;
Var RcvStream:TMemoryStream;
     SndStream:TMemoryStream;
     L:TStringList;
begin
   RcvStream:=TMemoryStream.Create;
   SndStream:=TMemoryStream.Create;
   L:=TStringList.Create;
   try
     If (RequestVariableCount>0) then begin
       GetRequestVarList(L,True);
       for i:=0 to L.Count-1 do begin
         if VariableIsUploadedFile(L[i]) then begin
           RcvStream.LoadFromFile(UploadedFileName(L[i]));
         end;
       end;
     end;
     AddResponseLn('Content-Disposition: inline; filename="data.txt" 
              Content-Type: application/txt;');
     AddResponseLn('');
     SndStream.LoadFromFile('FileToSendBack.txt');
     SndStream.Seek(0,soBeginning);
     Response.CopyFrom(SndStream,SndStream.Size);
   finally
     RcvStream.Free;
     SndStream.Free;
     L.Free;
     Terminate;
   end;
end;

begin
   With TYourNewCGI.Create(Nil) do begin
     try
       Title:='YourNewCGI';
       Initialize;
       Run;
     Finally
       Free;
     end;
   end;
end.


Regards
Lukas

-- 

--------------------------
software security networks
Lukas Gradl <fpc#ssn.at>
Eduard-Bodem-Gasse 9
A - 6020 Innsbruck
Tel: +43-512-214040-0
Fax: +43-512-214040-21
--------------------------



More information about the fpc-pascal mailing list