It may not be the cleanest of solutions, but at the moment we do what you're doing with a temp file: create the .XLS file in some random location, then read it back into memory (in the same script) and print it to the client (then, presumably, delete the temp file). This keeps everything in one process, so you can report errors directly to the client (and not bother forking), and also lets you defer printing the headers until you know more about your output (the size, of course, but also if there were any errors as you fetched the data).
That is:
use File::Temp 'tempfile';
use CGI 'header';
my ($fh,$filename) = tempfile();
my $workbook = Spreadsheet::WriteExcel->new($filename);
do_stuff($workbook);
$fh->seek(0,0);
my $data;
my $size = -z $filename;
$size== $fh->read($data,$size) or bail_the_heck_out(); #CGI::Carp migh
+t help
print header(
-type=>"application/vnd.ms-excel",
-content_length=>$size,
-attachment=>$file, # assuming you set that elsewhere
), $data;
If God had meant us to fly, he would *never* have given us the railroads. --Michael Flanders
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|