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

I am trying to open an excel file in browser using CGI::Application. I am giving my run mode code below. It only seems to print something like '2/8' in browser but isn't opening the excel file. Any help in resolving this is highly appreciated. Thanks in advance.
sub show_excel { my($self) = shift; my $file = "temp.xls"; my $output = $self->header_props(-type => 'application/vnd.ms-excel', -attachment => $file); return $output; }

the $file has 777 permissions and the file has data.
Prakash

Replies are listed 'Best First'.
Re: how to open excel in browser using CGI::Application
by bmann (Priest) on Jun 20, 2005 at 05:12 UTC
    First, CGI::Application won't automatically send the file. The script needs to read the file and return the data.

    Second, header_props is a function that modifies the header directly. There is no need to assign it to $output at all. The 2/8 you see is result of printing the anonymous hash (header) in scalar context.

    Here's a working example:

    sub show_excel { my $self = shift; my $file = "temp.xls"; my $output = do { local $/; open FH, 'temp.xls' or die "$!"; <FH> }; $self->header_props(-type => application/vnd.ms-excel', -attachment => $file); return $output; }

    Finally, it is up to the end user whether to open Excel outside of the browser, embedded in the browser or save the file to disk. A web page can't force it to open in the browser, if that is what you are looking for.

    HTH

Re: how to open excel in browser using CGI::Application
by Thilosophy (Curate) on Jun 20, 2005 at 09:15 UTC
    You could use CGI::Application::Plugin::Stream :
    use CGI::Application::Plugin::Stream (qw/stream_file/); sub show_excel{ if ( $self->stream_file( "temp.xls" ) ) { return; } else { return $self->error_mode(); } }