sushi2k has asked for the wisdom of the Perl Monks concerning the following question:
I don't know how I can create output to a html page and also send a xls file to the browser.
Here is the scenario:
I'm creating with Spreadsheet::ExcelWriter a xls file. This file should, after creating it, be send to the browser so that it can be downloaded. But I want also some Output about the creation of the xls file (if there were any errors but also if all went well). So I got one perl-script that creates the xls and sends it to the browser. This script should also create some output in a html file.
Here is the piece of code I've found via google that sends the xls ($fileName) to the browser so that I can save it (this works, if I don't have any other output):
### read file and send
my $file_size = -s qq{$fileName};
print CGI::header({
'content-type' => 'application/excel',
'content-length' => $file_size,
'content-disposition' => qq{attachment; filename=$fileName}
});
open my $XLS, qq{$fileName} or do {
my $self->_generate_response({
code => 500,
message => qq{file $fileName not found: $!},
});
return;
};
my $buffer;
while(read $XLS,$buffer,16384) {
print $buffer;
}
close $XLS;
If the script writes some output about the result of the creation of the xls file I get binary code after this output in my browser. And I can't download the xls file because it's written directly to the browser.
This piece of code is at the begining of the script and there are no more print commands when the xls is being send to the browser:
############################################################ #html output, that shows if the creation of the xls was successful print "Content-type: text/html\n\n"; print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">', "\n"; print "<html><head><title>Creating xls-output</title></head><body>\n"; print ($auditId." ".$auditName.");I thougt it would be solved if I set the header to application/excel after the html-output has finished and then the xls can be downloaded again. But I still get binary code in my browser. I think it's a problem with the header, but I don't know how to solve it.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Send file to Browser
by moritz (Cardinal) on Jul 30, 2009 at 15:06 UTC | |
|
Re: Send file to Browser
by psini (Deacon) on Jul 30, 2009 at 15:11 UTC | |
|
Re: Send file to Browser
by sushi2k (Novice) on Jul 31, 2009 at 10:55 UTC |