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

Hello,

I ran into problems trying to send binary content to the user's browser for download. Basically what is supposed to happen is when the user clicks on a link, a save as page opens, and the user can click save button to download the file. All of this happens, but I get a wrong file size in the end, so I know the file is not written correctly. Here's the code I'm using in my Perl / CGI script:

open(DB, "<$filename") or die "couldn't open $filename for download: $ +!"; print "Content-Title: $filename\n"; print "Content-Disposition: attachment; filename=$filename\n"; print "Content-Type: application/octet-stream; file=$filename\n\n"; binmode DB; binmode STDOUT; my $buff; while( read(DB, $buff, 1024) ) { print STDOUT $buff; } close(DB);

The file I'm trying to download is 40k in size, but when it's saved on the disk by the cgi script it's 45.7k.

Could anyone take a guess at what the problem might be? Or, maybe give me some pointers to where to find more information on downloading binary files on Win32 systems. I did look in the Camel, Cookbook, online perl docs, Q&A on CGI Programming on Perlmonks and tried a few quick searches in the newsgroups, but they either talk about the problems to which I already know a solution, or the information is for Perl scripts running on Unix systems.

Thank you,
Alex

Replies are listed 'Best First'.
Re: Binary file download on Win32
by relax99 (Monk) on Oct 17, 2002 at 14:40 UTC
    I'm terribly sorry. I have just solved my problem - I should stop getting up at such early hours :) The problem was in the flow of execution in the script, basically after printing the content of binary files to STDOUT, the script kept going and printed some unrelated data as well.
      Just a thought but its probably a good idea to set autoflush on for STDOUT.
      select((select(STDOUT),$|++)[0]);

      --- demerphq
      my friends call me, usually because I'm late....