如何通过setTimeout在javascript中制作自动下载图像?

时间:2016-08-15 06:46:51

标签: javascript

<a id="download" href="link of your file">click here to download the file</a>.

<script>
    var downloadTimeout = setTimeout (
        function() {
            window.location = document.getElementById('download').href;
        }, 4000);
</script>

这不是我想要的,我需要将图像自动下载     时间是由setTimeout停止但我无法得到它。请帮我。谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用HTML5进行操作。要下载图片,请将download媒体资源添加到您的链接中。请注意,这在某些浏览器中不起作用。您可以在JSFiddle上试用。

<a href = "yourImage.png" download = "customName.png" id = "download">Click here to download image</a>
<canvas></canvas>
<script>
  setTimeout(function(){
    downloadCanvas();
  }, 4E3);

  function downloadImage(){
    document.getElementById('download').click();
  }

  function downloadCanvas(){
    var a = document.getElementById('download');
    var b = a.href;
    a.href = document.getElementsByTagName('canvas')[0].toDataURL();
    downloadImage();
    a.href = b;
  }
</script>