[英]jQuery Save Image Onclick
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.在我的网站上,我有一个 jQuery 脚本,如果单击下载按钮,它将在新的 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.我的问题是,当您单击按钮时,我如何制作此脚本图像将自动保存并且不会在新的 window 中打开。
My code:我的代码:
<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首先,我尝试jqueryfiledonwloader但不适用于图像文件,经过一番搜索后,我找到了以下解决方案,这对我来说很有用,试试这个
<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>
<button class="btn btn-success" style="width: 250px;" data-image="/media/result/online_resultjpg_Page68.jpg" data-exam="GMO" data-roll="110211" onclick="downloadCertificate(this);" >Download Certificate</button> <script type="text/javascript"> function downloadCertificate(obj){ var link = document.createElement('a'); const image = $(obj).data("image"); const img_ext = image.split("."); const ext = img_ext[img_ext.length - 1]; link.href = image; link.download = $(obj).data("exam") + "_" + $(obj).data("roll") + ext; document.body.appendChild(link); link.click(); } </script>
Yuseferi script solution could be transformed into a simple anchor tag we put before the closing of the #download-btn
one: Yuseferi 脚本解决方案可以转换为我们在
#download-btn
关闭之前放置的简单锚标记:
<a href='/sites/default/files/toy/jpeg/image-1419683919_4851.jpeg'
download='MyToy.jpeg'>MyToy.jpeg</a>
By the way, download
is a standard HTML5 anchor tag (MDN) attribute and, in script , it is flagged "experimental": maybe because of unwanted side effect.顺便说一下,
download
是一个标准的 HTML5锚标记 (MDN)属性,在script 中,它被标记为“实验性”:可能是因为不需要的副作用。 Still Yuseferi solution should be full proof.仍然 Yuseferi 解决方案应该是充分的证明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.