jQuery保存图片Onclick [英] jQuery Save Image Onclick

查看:119
本文介绍了jQuery保存图片Onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我有一个jQuery脚本,如果单击下载按钮,它将在新窗口中打开所需的图像.

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.

我的代码:

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

推荐答案

首先,我尝试

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>

这篇关于jQuery保存图片Onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆