Skeeve has asked for the wisdom of the Perl Monks concerning the following question:

Please see Dancer and Excel::Writer::XLSX incompatibility! for the real solution and the root cause.


SOLVED!

binmode($fh);

It seems my Dancer application is injecting some CR/LF otherwise...


I'm at my wit's end...

I have a Dancer application where I want to download an Excel file created with Excel::Writer::XLSX as it is the successor of Spreadsheet::WriteExcel.

The below given test code works perfectly. I can download the example XLSX file. But as soon as I take the route and put it into my application, without changing it at all, I receive a non-functional xlsx file. I even tried to unpack it (as these files are zipped) and this unpacking already fails.

I have absolutely no idea what's going on there and I'm wondering whether or not anyone of you ever experienced something like this and might be able to point me into the right direction to solve this proble,m.

use Dancer; use utf8; use Excel::Writer::XLSX; # Step 0 get '/xxx' => sub { my $buffer; open my $fh, '>', \$buffer; my $workbook = Excel::Writer::XLSX->new( $fh ); # Step 1 my $worksheet = $workbook->add_worksheet(); # Step 2 $worksheet->write( 'A1', 'Hi Excel!' ); # Step 3 $workbook->close(); close $fh; return send_file( \$buffer, content_type => 'application/vnd.openxmlformats-officedocument +.spreadsheetml.sheet', filename => 'xxx.xlsx', ); }; dance;

s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re: Dancer and Excel::Writer::XLSX incompatibility?
by Corion (Patriarch) on Jan 28, 2014 at 12:59 UTC

    send_file() needs a filename. It is not intended (I think) to send stuff that does not live on disk.

    I think the best approach would be to simply send a normal response, with $buffer in the body.

      That would not explain, why the test I posted works.

      And the documentation states:

      If you have your data in a scalar variable, send_file can be useful as well. Pass a reference to that scalar, and send_file will behave as if there was a file with that contents

      P.S.: I also tried working with a temporary file. It was the same result. Both, the file downloaded and the temporary one on the server, were corrupt.


      SOLVED!

      binmode($fh);

      It seems my Dancer application is injecting some CR/LF otherwise...

      Thanks for looking into it!


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e