jQuery Save Image Onclick

匿名 (未验证) 提交于 2019-12-03 08:59:04

问题:

On my website I have a jQuery script that, if the download button is clicked it will open the image that you want in new window.

My question is, how can I make this script when you click the button the image will save automatically and not open in a new window.

My code :

<script type="text/javascript">         $(document).ready(function (){             $('#download-btn').click(function(){                 var size = $('#size').val();                                 window.open(size);             });         })     </script> 

回答1:

First I try jqueryfiledonwloader but not work on image file,after a some searching I found below solution,This work for me charmly,try this

 <script type="text/javascript">         $(document).ready(function (){             $('#download-btn').click(function(){      var link = document.createElement('a');                   link.href = '/sites/default/files/toy/jpeg/image-1419683919_4851.jpeg';  // use realtive url                    link.download = 'MyToy.jpeg';                   document.body.appendChild(link);                   link.click();                 });         })     </script> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!