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

Mark Knoop mark at rawcane.net
Wed Nov 26 14:07:23 EST 2008


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:

######
...

my $dl_file = '/path/to/something.ext';
my $file_name = 'something.ext';
open(FILE, "< $dl_file") or $log->logdie ("Couldn't open file $dl_file");
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, 
$atime,$mtime,$ctime,$blksize,$blocks) = stat(FILE);
binmode(FILE);

print "Content-type: application/octet-stream\n";
print "Content-length: $size\n";
print "Content-disposition: attachment;filename=$filename\n\n";

while(<FILE>) {
   print;
}

close FILE;

exit;
########

This works and enables one to wrap other stuff around the download of a file 
rather than just linking to the file directly.

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.

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?

Cheers
Mark





More information about the cgiapp mailing list