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

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);
  • Comment on header (-type=>'application/x-octet-stream') and header (-type=>'plain/html') don't play nicely together
  • Download Code

Replies are listed 'Best First'.
Re: header (-type=>'application/x-octet-stream') and header (-type=>'plain/html') don't play nicely together
by friedo (Prior) on Nov 10, 2006 at 20:35 UTC
    You can't send two completely different things to the browser as the same time. If you want to display some HTML while a file is downloading, send a plain HTML page with a redirect to the file download script.
Re: header (-type=>'application/x-octet-stream') and header (-type=>'plain/html') don't play nicely together
by Fletch (Bishop) on Nov 10, 2006 at 20:37 UTC

    Presuming I get what you're asking . . . erm, you can't do that. The usual method of doing this is to provide a refresh (e.g. print a <meta http-equiv="refresh;blah"> (whatever the actual header is; that's not it) header) that sends the browser to the download location after you've displayed whatever content you want the user to see.