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


In reply to Binary file download on Win32 by relax99

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.