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 );
}
############################################################################
###
 
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top