Hy perlmonks,

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.
Thx in advance.

In reply to Send file to Browser by sushi2k

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.