Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Need help with downloading

by gl2103 (Novice)
on Apr 18, 2005 at 17:40 UTC ( #448953=perlquestion: print w/replies, xml ) Need Help??

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

Hello everybody, I'm really new to PERL and CGI and really need some help. I'm trying to wrote some script to force download files from server. It looked not too hard but I stocked there. I always have the following messages when opening files:
xls: "This file is not in recognizable format"
pdf: "There was an error opening this document.The file is damaged..."
All files are exist and can be opened with no problem. I don't know what to do.Please help. Thanks.
Here is my code:
------------------------------------------------------
....
#dynamically determine $outf="C:/folder1/test.xls; #dynamically determine $filenm="test.xls"; print "Content-Type: application/octet-stream\n"; print "Content-Disposition:attachment;filename=$filenm","\n\n"; print "Content-Length: $no_bytes","\n\n"; open(FILE,"<$outf"); while(<FILE>){print;} close (FILE);
-------------------------------------------------

Updated Steve_p - added code tags

Replies are listed 'Best First'.
Re: Need help with downloading
by Fletch (Chancellor) on Apr 18, 2005 at 17:54 UTC

    From the looks of things you're on Wintendo so a perldoc -f binmode may be helpful.

      aye, adding binmode(FILE); binmode(STDOUT); after the open should fix it.

      Update: Typo fixed.

        binmode on both :-|

        Alberto Simões

      Where do you want to do this? Sorry for asking a stupid things.Thanks
        I'm a little confused. Could you please change this for me?
        Thanks
        ------------old----------------------
        open(FILE,"<$outf");
        while(<FILE>){print;}
        close (FILE);
Re: Need help with downloading
by eibwen (Friar) on Apr 18, 2005 at 19:10 UTC
    A quick google search returned a PHP varient. Transcribing it into perl and extending the functionality:

    #!/usr/bin/perl -w use strict; use File::Type; my $file = $0; print "Pragma: public\n"; # reportedly required print 'Content-Type: ', File::Type->new()->mime_type($file) || 'applic +ation/force-download', "\n"; print "Content-Disposition: attachment;filename=$file\n"; print "Content-Transfer-Encoding: binary\n"; print 'Content-Length: ', -s $file, "\n\n"; open(FILE, "$file"); binmode $_ foreach (*FILE, *STDOUT); print foreach (<FILE>); close(FILE);

    Verified to work with Apache on Linux.

    UPDATE: Removed unnecessary variables

Re: Need help with downloading
by ambs (Pilgrim) on Apr 18, 2005 at 17:44 UTC
    Never tried to do this, but my instinct says it should be:
    print "Content-Disposition:attachment;filename=$filenm","\n";
    and not
    print "Content-Disposition:attachment;filename=$filenm","\n\n";

    Alberto Simões

      Thanks Alberto.I tried but it still doesn't work

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://448953]
Approved by jbrugger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2020-11-25 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?