Sending a binary file through a script (binmode/stdout/mime type)


A

Alain Star

Hello,

I am trying to write a routine wich, when called from a browser, give the
possibility to download a file.
I have almost succeded but, the only things wich do not work properly is:
- there is no dialog box to choose save / open
- the saved name of the file is wronf (download.cgi.mp3)

Any idea what I am doing wrong? Thanks!

Following, example URL, sub.

http://www.cri.ch/cgi-bin/download.cgi?file=dassault.mp3&send=1

############################################################################
###
sub sendMP3 {

my $file = '/home/sites/site7/data/download/dassault.mp3' ;
open( TUNE, "< $file " ) ;
binmode TUNE ;

my $tunesize = ( stat(TUNE) )[7];

binmode STDOUT ;

print $q->header( -type => 'audio/x-mp3',
-Content_length => $tunesize
);

my $buf; while ( read(TUNE, $buf, 32768) ) { print STDOUT $buf; }

close(TUNE);
exit( 0 );
}
############################################################################
###
 
Ad

Advertisements

B

Brian McCauley

Alain said:
Hello,

I am trying to write a routine wich, when called from a browser, give the
possibility to download a file.
I have almost succeded but, the only things wich do not work properly is:
- there is no dialog box to choose save / open
- the saved name of the file is wronf (download.cgi.mp3)

Any idea what I am doing wrong?

You are posting non-Perl-related questions to a Perl newsgroup. (How do
you think the answer to you question would be different if your CGI was
in C, Java, Bourne-shell, Pascal, FORTRAN...).

Take a look at the Content-disposition HTTP header. A google seach form
content-dispostion should also help you find work-rounds for various
browsers' ideosyncracies.
my $file = '/home/sites/site7/data/download/dassault.mp3' ;
open( TUNE, "< $file " ) ;
binmode TUNE ;

my $tunesize = ( stat(TUNE) )[7];

binmode STDOUT ;

print $q->header( -type => 'audio/x-mp3',
-Content_length => $tunesize
);

my $buf; while ( read(TUNE, $buf, 32768) ) { print STDOUT $buf; }

close(TUNE);

It is much better simply to generate an internal redirect and let the
web server serve the file directly (taking, if necessary, appropriate
measures in the server config to ensure that the download directory
cannot be accessed bypassing the script).
 

Top