[dancer-users] Failing to send a file in iso-8859-1

Lutz Gehlen lrg_ml at gmx.net
Wed Oct 5 17:59:26 BST 2016


Hello everyone,

I would like to send the CSV content of a scalar variable as a file
to the browser by using send_file in Dancer1. My sample code looks
like this:

send_file(
    \$str,
    content_type => 'text/csv',
    filename     => 'foo.csv',
);

The problem is that external requirements force me to send the file
in iso-8859-1 and it seems that I cannot manage to achieve that.

My first attempt was to just encode the content (which I should do
anyway, I guess):

$str = Encode::encode('iso-8859-1', $str);
send_file(
    \$str,
    content_type => 'text/csv',
    filename     => 'foo.csv',
);

However, it seems like the content gets somehow magically re-encoded
to UTF-8. Moreover, the browser receives a content type header of
Content-Type:text/csv; charset=utf-8

So I have tried the following variants:

$str = Encode::encode('iso-8859-1', $str);
send_file(
    \$str,
    content_type => 'text/csv',
    charset      => 'iso-8859-1',
    filename     => 'foo.csv',
);

----

$str = Encode::encode('iso-8859-1', $str);
send_file(
    \$str,
    content_type => 'text/csv',
    encoding     => 'iso-8859-1',
    filename     => 'foo.csv',
);

----

$str = Encode::encode('iso-8859-1', $str);
send_file(
    \$str,
    content_type => 'text/csv; charset=iso-8859-1',
    filename     => 'foo.csv',
);

The first two do not seem to change anything, the last one results
in a content type header of
Content-Type:text/csv; charset=iso-8859-1; charset=utf-8

In all cases, the content of the file is UTF-8 encoded.

Does anyone know how I can fix this issue?

Thanks a lot and best wishes,
Lutz


More information about the dancer-users mailing list