Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

File Download from Web

by jiewho (Novice)
on Mar 12, 2004 at 01:05 UTC ( #336048=perlquestion: print w/replies, xml ) Need Help??

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

My cgi program generated a file. I want to put this file as a link on the web so that users can click the link to download. This seems to be such a common task on the web. Would anybody please show me a script example? When I searched CPAN for perl modules, it always points me to the lwp-download. To do this on the command line is straight forward, but how do I put this command in a url? Thanks,

Replies are listed 'Best First'.
Re: File Download from Web
by TomDLux (Vicar) on Mar 12, 2004 at 01:28 UTC

    How about simply putting a URL on a web page linking to the text file:

    <a href="file.txt">My newly generated file</a>


Re: File Download from Web
by esskar (Deacon) on Mar 12, 2004 at 02:38 UTC
    lets say your script is generating a gif
    #!/usr/bin/perl # use strict; use warnings; my $gif = pack("H*", '47494638396101000100B30000888888' . 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' . 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' . 'FFFFFFFFFFFFFFFFFFFFFFFFFF2C0000' . '0000010001000004021044003B' ); print "Content-Type: image/gif\n\n"; binmode STDOUT; print $gif;
    then just add to your web page as pointed out above
    <a href="">A Gif</a>
Re: File Download from Web
by jiewho (Novice) on Mar 12, 2004 at 08:03 UTC
    Thanks for the input. Both methods are to have the file displayed in the browser window. But this is not what I meant. When a user clicks the link, I want a file download dialogue window to pop up, so the user can save this file to the local computer. This automatically happens when the file is a .tar.gz type file. But if the file is a .txt file, it's displayed in the browser window. So is there a way not to have the .txt file displayed but to pop up a file download dialogue window when the link is clicked? Thanks
      Your program needs to send the file to the user itself. When you click a link you are linking to the program and not to the file, something like:
      <a href="/yourprogram.cgi">Click here</a>
      The program must spit out the correct headers. One of these should probably be 'content-disposition' so I suggest you poke around based on that.

      Be wary of specifiying the file in the link unless you know EXACTLY what you are doing, otherwise this can happen:
      <a href="file.cgi?file=../../../etc/passwd">here</a>
      Ok thats a contrived example to indicated what I mean but that should indicate the problems you face ;). If you are not careful someone could download any file on your system which is something you definitely wouldn't want :).
      okay... then use
      print "Content-Type: application/octet-stream\n\n";
Re: File Download from Web
by wolfi (Scribe) on Mar 12, 2004 at 09:55 UTC
    not really perl-solutions, but after some thought -
    well... changing the mime-types won't work, since some browsers disregard this in favour of their own results from their pre-scanning of the file - and changing the extension of the file would probably confuse visitors... or scare'em.
    setting up some ftp-thingee might be a thought - but really ->
    the easiest thing you can do is to compress it (if you don't wanna use tar.gz - use WinZip - or better yet, have both options available). Zipped files are always (i would think) downloaded like ya want.
    OR... simply put a link to the document and tell your users, that if they'd like a copy - they should right-click on the link and select SAVE TARGET AS (or whatever). That'd be the easiest.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://336048]
Approved by arden
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2023-05-30 10:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found