Saturday, August 19, 2006

Server zip file in Catalyst with IO::Scalar

Thank God, Thank IO::Scalar.

u know we can transfer a filehandle to $c->res->body. and IO::Scalar is making my life easy.

just a snippet code, have fun.
sub test : Local {
my ( $self, $c ) = @_;

use Archive::Zip qw(:ERROR_CODES :CONSTANTS);

my $zip = Archive::Zip->new();

$c->res->content_type('application/octet-stream');
$c->res->headers->header( 'Content-Disposition' => "attachment; filename=\"fayland.zip" );

use IO::Scalar;
my $data;
my $fh = new IO::Scalar \$data;

$zip->addFile('E:/Fayland/index.html') or die $!;
$zip->writeToFileHandle($fh);

$c->res->body($data);
}
IO::Scalar 能将一个 $scalar 作为 filehandle IO 来对待。在 Catalyst 里将是特别有用的。

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home