<script src="https://AJAX.GoogleAPIs.com/AJAX/libs/jquery/1.11.3/jquery.min.Js"></script><script type="text/JavaScript" src="HTML2canvas.Js"></script><button ID="save_image_locally">download img</button> <div ID="imagesave"> <img ID='local_image' src='img1.jpg'> </div><script> $('#save_image_locally').click(function(){ HTML2canvas($('#imagesave'),{ onrendered: function (canvas) { var img = canvas.toDataURL("image/png"); alert('This will currently open image in a new window called "data:". Instead I want to save to users local computer. IDeally as a jpg instead of png.'); window.open(img); } }); });</script>解决方法 尝试这样(请注意,Safari会在同一个标签页中打开图片,而不是下载它; Safari中不支持下载属性)
<script> $('#save_image_locally').click(function(){ HTML2canvas($('#imagesave'),{ onrendered: function (canvas) { var a = document.createElement('a'); // toDataURL defaults to png,so we need to request a jpeg,then convert for file download. a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg","image/octet-stream"); a.download = 'somefilename.jpg'; a.click(); } }); });</script>总结
以上是内存溢出为你收集整理的如何使用HTML2canvas将img保存到用户的本地计算机全部内容,希望文章能够帮你解决如何使用HTML2canvas将img保存到用户的本地计算机所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)