[cgiapp] How to avoid delay when writing file in bytes

Rhesa Rozendaal perl at rhesa.com
Wed Nov 26 14:29:02 EST 2008


Mark Knoop wrote:
> Hi
> 
> I want to bring some file download procedures into my CGI::Application. In 
> the past (in normal CGI scripts) I have done this by using something like 
> the following:
> 
> ######
> while(<FILE>) {
>    print;
> }
> ########
> 
> I would like to do the same inside my CGI::App but of course I am not 
> allowed to print directly to STDOUT so following this method I would have to 
> suck in the whole file first before I return the output to be handled by run 
> which causes an unacceptable delay if the file is large.


Of course you're allowed to write to STDOUT! You just need to let cgiapp know 
that you're doing so, by setting the header_type to 'none', and returning an 
empty string back to cgiapp:

sub some_runmode {
   ...
   $self->header_type('none'); # we'll do this ourselves:
   print $my_custom_headers;

   while (<FILE>) { # not sensible with binary files. use (sys)read.
     print;
   }

   return ''; # we already sent the body
}


> So... how to approach this? Is there a way I can push out a file as a 
> download from within CGI::Application without having to suck the whole thing 
> into memory first?


An even nicer approach is to use CGI::Application::Plugin::Stream.

rhesa


More information about the cgiapp mailing list