简体   繁体   中英

Javascript for saving link using ctrl+s

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()" />

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM