0

When a user clicks on image link, it should prompt save option (instead of right click and save). I found ways to alert the user if he press ctrl +s. But how do I show an save option when he click the link. Below is my code which is not working.

<script type="text/javascript">
function myFunction()
{
   if  (event.ctrlKey && event.keyCode == 115)
   {
       event.keyCode = 0;
   }
}
</script>

<input type="button" onclick="myFunction()" />
Abhishek
  • 6,912
  • 14
  • 59
  • 85
PHP
  • 445
  • 1
  • 10
  • 20

1 Answers1

2

You cannot manually open a save-as prompt in Javascript. The only thing you could do is change the window's location to the image, which you serve with special headers from the server. For example:

<img onclick="saveFunction()">

<script type="text/javascript">
    function saveFunction() {
        window.location.href = this.getAttribue('src') + '?somethingspecial';
    };
</script>

Look at this thread to see how to serve a file to prompt as download. Looks like you need to set the Content-Disposition header with whatever web server you are using.

Community
  • 1
  • 1
Andy Ray
  • 30,372
  • 14
  • 101
  • 138