How does one modify a script which functions satisfactorily in permitting a user to download files (from the server to the client using "save as"), so that some plain text/html can also be added into the application without said additions corrupting the operation of the download?

Consider the below script which works fine as long as one only wants to use it to download a file on the server.

But suppose we want the script to also do things like say 'Hello World' and 'Goodbye World' (at the places commented out in the script)?

My dilemma is header (-type=>'application/x-octet-stream') and header (-type=>'plain/html') don't play nicely together. I've researched perldoc CGI headers and write-ups about the RFC on HTTP to no avail. Monks please advise.

#! /usr/bin/perl -wT use strict; #force all variables to be declared before use use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); print header (-type=>'application/x-octet-stream', -attachment=>'test. +bmp'); start_html (); my $file_name = "file1"; open (INFILE, $file_name) || die ("Can't open ($file_name): $!"); ############################ # print "Hello World as plain text/html before downloading ]<br>\n"; ############################ binmode INFILE; # allow FILEHANDLE read in binary mode $/ = undef; # read entire file not line by line, but in slur +p mode where entire file is read into scalar or array my $data = <INFILE>; # read it into scalar variable close (INFILE); # close input file binmode (STDOUT); # allow FILEHANDLE write in binary mode print $data; # write file to FILEHANDLE STDOUT ############################ # print "Goodbye World as plain text/html after downloading ]<br>\n"; ############################ print end_html (); exit (0);

In reply to header (-type=>'application/x-octet-stream') and header (-type=>'plain/html') don't play nicely together by gmacfadden

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.