Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I download a file automatically without click on button?

Tags:

html

download

I am trying to write code that opens a clean page that downloads a file when I open it. My problem is that I didn't find how to do it automatically. I only find this code:

<!DOCTYPE html>
<html>
<body>
<a href="content/file.jpg" download > jpg link </a>
</body>
</html>
like image 433
Fox Avatar asked Sep 22 '16 19:09

Fox


2 Answers

Without using a backend language, you can add this jQuery to your HTML document.

<script type="text/javascript">
    $(document).ready(function () {

        $("a").click();

    });
</script>

The link will be automatically clicked as soon as the document has loaded.

like image 174
The One and Only ChemistryBlob Avatar answered Nov 15 '22 03:11

The One and Only ChemistryBlob


I hope this will works all the browsers. You can also set the auto download timing.

<html>
<head>
<title>Start Auto Download file</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$(function() {
$('a[data-auto-download]').each(function(){
var $this = $(this);
setTimeout(function() {
window.location = $this.attr('href');
}, 2000);
});
});
</script>
</head>
<body>
<div class="wrapper">
<p>The download should start shortly. If it doesn't, click
<a data-auto-download href="auto-download.zip">here</a>.</p>
</div>
</body>
</html>

Demo Here

like image 28
sansan Avatar answered Nov 15 '22 04:11

sansan