[Catalyst] Catalyst::Response - send a file

Carl Franks fireartist at gmail.com
Tue Aug 19 14:41:40 BST 2008


2008/8/19 Dermot <paikkos at googlemail.com>:
> 2008/8/19 Carl Franks <fireartist at gmail.com>:
>> 2008/8/19 Dermot <paikkos at googlemail.com>:
>>> sub downloadFile {
>>>
>>>  my ($name, $filepath) = @_;
>>>
>>>  my $length = (stat($filepath))[7];
>>>  my $res = $c->response;
>>>  $res->content_length($length);
>>>
>>>  $res->headers->({ 'Content-Disposition' =>
>>> "attachment;filename=$name"} ); # CODE ref error
>>>
>>>  $res->sendfile($filepath,0,$length); # Ok no such method but
>>> hopefully you'll get what I mean.
>>>
>>> }
>>
>> open my $filehandle, '<', $filepath
>>    or die $!;
>>
>> $c->response->body( $filehandle );
>
>
> Sorry. I mustn't be making myself clear.
>
> I want the browser to offer the user a 'save as' dialogue box.  The
> modperl equivalent would be the RequestIO::sendfile
>
> The above sends the contents of filepath to the browser

Ah, ok.
You should have used header().
headers() returns the HTTP::Headers object, which you were overwriting.

$c->response->header(
    'Content-Disposition' => "attachment;filename=$name"
);

You'll still need the body($fh) bit to send the data, too.

Carl



More information about the Catalyst mailing list