in reply to Getting CGI script to send a binary file

I have tried this (from cgi-multi-download):
my $boundary_string = "\n" . "--End" . "\n"; my $end_of_data = "\n" . "--End--" . "\n"; my @file_list = ("wlmlm401.exe"); print <<EOH; Content-type: multipart/x-mixed\;boundary=End EOH foreach my $file ( @file_list ) { &send_file( $file ); print $boundary_string; } print $end_of_data; exit(0); sub send_file { my $file = $_[0]; if ( open (FILE, "< $file")) { print <<EOF; Content-type: application/octet-stream Content-disposition: attachment\; filename=$file EOF binmode FILE; print <FILE>; close (FILE); } else { print "Cannot open file $file!"} }
With the file in the cgi-bin directory I click on the link to the script and I get a blank browser page with no 'save as' dialogue.

So I tried merlyn's suggestion:

$/ = undef; @ARGV = ("/home/goldcal/www/files/wlmlm401.exe"); print multipart_init(); while(<>) { print multipart_start("application/octet-stream"); print $_; print multipart_end(); } print multiplart_final(); ## UPDATE - THis part should not have been here, it is finger trouble +;) #print $cgi->header( -type => "application/octet-stream", # -expires => "-1d", # -attachment => "wlmlm401.exe", # );
Which is as singularly unsuccesful.

I am using Firefox 1.0 as my browser, but I did try it in IE and it works equally unwell.

jdotoronto

Replies are listed 'Best First'.
Re^2: Getting CGI script to send a binary file
by graff (Chancellor) on Feb 08, 2005 at 05:10 UTC
    Um, in your first attempt, it looks like you forgot to do "binmode STDOUT"?

    And in the code you showed for trying merlyn's suggestion, it looks like you're printing the content header after printing the file (and you're not even doing "binmode" on the input, let alone on STDOUT).

    Was that just a flaky copy/paste into the post?

    update: Sorry, I'm probably missing the point completely here. In the first snippet, it's unclear to me why zentara (the original author) would do binmode on input and not on output -- I assumed that if you need binmode at all, you need it on both. As for the second snippet, merlyn was apparently assuming a OS environment where binmode was not necessary at all, and was using CGI.pm's functions for doing the content headers, boundaries, etc (so you shouldn't have been printing a header after that loop).