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

Can I create buton in my application for 'save Page As...' where it is the same if I press Ctrl S, but the button is in my application and I click it with my mouse. I know it not necessary but I would like to create it so that even kid can use my application.

Replies are listed 'Best First'.
Re: Ctrl S function in PERL
by legato (Monk) on Jan 06, 2005 at 15:59 UTC
    I assume you're using Perl/Tk to create your GUI. If so, you want to use the bind method on the menu item widget.

    Anima Legato
    .oO all things connect through the motion of the mind

      It is a web based application, so I am using HTML for the interface...

        Update: Sorry, I thought you were asking how to trap Control-S in your webpage to save the file. Oh well, back to the white powder I found on the floor...

        Then you will have to use client side javascript. It can be done, but not without some effort. You will need to look at trapping key events. For example, you can look at: http://tech.irt.org/articles/js195/

        Also remember, you will want to test this on different browsers with different versions due to the differences/bugs in various Javascript implimentations. And of course, many people have javascript off and don't even know it.

        Ted Young

        ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re: Ctrl S function in PERL
by punch_card_don (Curate) on Jan 06, 2005 at 17:11 UTC
    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.

      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. :-)