[Catalyst] Out of Memory - File delivery issue
Lukas Thiemeier
spamcatcher at thiemeier.net
Thu May 2 18:03:37 GMT 2013
On 05/02/2013 06:19 PM, Craig Chant wrote:
> I can't work this out?
>
> I have in my model...
> ------------------------------------------------------------------
> my $xls = "col1,col2,col3\n";
>
> # open io handle
> $io_handle = IO::Handle->new();
> open my ($str_fh), '>', \$xls;
>
> if ($io_handle->fdopen($str_fh,"w"))
> {
> $io_handle->print('"row1","row2","row3"' . "\n");
> }
>
> return $io_handle;
> --------------------------------------------
>
> in my controller...
> --------------------------------------------------------------
> # output header
> $c->response->header(
> Content_Type => 'application/vnd.ms-excel',
> Content_Disposition => 'attachment;filename=NBCS_Export.csv'
> );
>
> # output XLS data
> $c->response->body($io_handle);
> ----------------------------------------------------------------------
>
> All I get is a blank XLS file?
>
> I can't work out how I create the IO::Handle object with the XLS data inside it?
>
> Thanks,
>
> Craig.
>
Hi Craig,
I don't have much experience with IO::Handle. Additionally, I don't know
how your XML Model works. I assume that $xls will contain the generated
data. In that case, you should open $xls for *reading* and not for
*writing*. You have already written the data to $xls, so IO::Handle has
to read it.
You might want to take a look at IO::File or IO::String instead of using
IO::Handle directly. The following works for me:
use IO::File;
sub test :Local{
my ($self, $c) = @_;
$c->res->content_type("text/plain");
$c->res->header(Content_Disposition =>
'attachment;filename=test.txt');
my $data = "foo\nbar\n";
my $ioh = IO::File->new;
$ioh->open(\$data, "r");
$c->res->body($ioh);
}
There might be a better way. As I said, I am not an expert in using IO::*.
And don't forget: This is the Catalyst Users list, and not the
IO::Handle list. :)
cheers, Lukas
More information about the Catalyst
mailing list