On Tue, Jul 2, 2013 at 2:59 AM, Craig Chant
<cr...@homeloanpartnership.com>wrote:

>
>         # output header
>         $c->response->content_type('application/vnd.ms-excel');
>         $c->response->content_length(length($xls));
>         $c->response->header(Content_Disposition =>
> 'attachment;filename=NBCS_Export.csv');
>
>         # create an IO::File for Catalyst
>         use IO::File;
>         my $iof = IO::File->new;
>
>         $iof->open(\$xls, "r");
>         $iof->binmode(":encoding(UTF-8)");
>
>         # output XLS data
>         $c->response->body($iof);
>

All the above seems overkill.   I suspect what you want is closer to this:
(but see notes below).

        $c->response->content_type('text/csv');
        $c->response->body($xls);
        $c->response->header(Content_Disposition =>
'attachment;filename=NBCS_Export.csv');

Then with that content type the plugin would encode $xls as utf8 and add
;charset=utf8 (or whatever it is configured to encode as).

Notes:

First, you are not returning Excel, so the content type is not what you
first listed above, right?

Second, be aware that $c->response->content_length(length($xls)); could be
very wrong.  If $xls is really CSV text AND it's decoded then length($xls)
is the length in characters, not octets.   Don't set the content length.


Third, Catalyst::Plugin::Unicode::Encoding, IMO, has some issues.

The plugin limits to just these content types.

    return $c->next::method(@_)
      unless $c->response->content_type =~ /^text|xml$|javascript$/;

Then it does this:

    $c->response->body( $c->encoding->encode( $body, $CHECK ) )
        if ref(\$body) eq 'SCALAR';

Personally, I think the correct approach is to only encode *character* data
-- that is check to see if the utf8 flag is set before calling encode.

Maybe limit to the content types listed above, but throw an exception for
other content types where the body is a scalar AND has the utf8 flag on.
 After all, we can only write out octets or else we get the Wide Character
error.






-- 
Bill Moseley
mose...@hank.org
_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Reply via email to