in reply to Ctrl S function in PERL

Well, since you've asked in a Perl forum, here's a Perlish solution:

Write a Perl script to load the html page in question and return it with a content disposition header. Then make your button call that Perl script.

Your html Page:

<html> ....... <a href="../cgi-bin/my_download_script.pl?url_of_this_page"><img src=" +button.gif"></a> ....... </html>

Your Perl script, in pseudo code:

# get url substring from $ENV{'QUERY_STRING'} $filename = $ENV{'QUERY_STRING'} open(FILE, $filename) or dienice("cannot open file $filename : $_[0] $ +!"); @LINES = <FILE>; close(FILE); print "Content-Disposition: attachment; filename=$filename\n\n"; for $i (0..$#LINES) { print $LINES[$i]; }
The content disposition header as "attachment" forces the browser to open a dialog asking whether you want to open it or save it. Choosing "Save" opens the "Save as" dialog.

There are many possible variations on the html and the Perl. It could have been a form button instead of an image. You could use 'while' instead of loading into an array, you could use javascript to pass the url string so that you could create a standard button routine that just needs to be cut and paste into every page without custom-coding the url substring....

Forget that fear of gravity,
Get a little savagery in your life.

Replies are listed 'Best First'.
Re^2: Ctrl S function in PERL
by TedYoung (Deacon) on Jan 06, 2005 at 17:29 UTC

    A few other notes on this:

    Make sure you also print your content-type header. It should probably go before the content-disposition. If it does, then remember you only want one \n at the end:

    print "Content-type: text/html\n";

    For anyone else that might be doing this with files like PDFs, imgs, etc. here are a couple of other tips:

    If you have the file already on the disk, you should also print a Content-length header with the length of the file:

    print "Content-length: ", -s $file), "\n";

    And for binary files, you should probably call:

    binmode STDOUT;

    Just in case you script gets run on Windows or the like.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)