I want that as soon as my .html file is opened , a pdf starts to download automatically (support for pc , tablets, phones) . I am not sure what am I doing wrong ? Thank you . Any sure short JavaScript is more than wele
<a href = "/public/news.pdf" download> </a>
I want that as soon as my .html file is opened , a pdf starts to download automatically (support for pc , tablets, phones) . I am not sure what am I doing wrong ? Thank you . Any sure short JavaScript is more than wele
<a href = "/public/news.pdf" download> </a>
Share
Improve this question
asked Feb 7, 2018 at 5:53
KNIGHT MAHAJANKNIGHT MAHAJAN
4911 gold badge5 silver badges12 bronze badges
4
-
1
I think what you actually want is a http redirect from your
.html
url to the pdf. – Bergi Commented Feb 7, 2018 at 6:11 - @knightMahajan check my answer – Bachcha Singh Commented Feb 7, 2018 at 6:20
-
As @Bergi said you can try
<meta http-equiv="refresh" content="3;url=/public/news.pdf" />
in this case it will work even without javascript. – seenukarthi Commented Feb 7, 2018 at 6:38 - 1 Possible duplicate of Clicking on download icon and another page should open that displays message and download should start – seenukarthi Commented Feb 7, 2018 at 6:41
4 Answers
Reset to default 3Use this:
HTML
<a id="downloadLink" href="news.pdf" download></a>
Java Script
Solution 1 :
<script>
window.onload = function() {
document.getElementById('downloadLink').click();
}
</script>
Solution 2: after 2 second
<script>
var downloadStartTime = setTimeout(function () {
document.getElementById('downloadLink').click();
}, 2000);
</script>
window.onload function is that you are looking for.
window.onload = function() {
var url = "Your file url";
var w=window.open(url, '_blank');
w.focus();
};
This will help you
$(document).ready(function() {
var downloadUrl = "your_file_url";
setTimeout("window.location.assign('" + downloadUrl + "');", 1000);
});
using javascript
<iframe id="download_iframe" style="display:none;"></iframe>
<script>
function Download() {
document.getElementById('download_iframe').src = "//somewhere.abc/somefolder/something.exe";
};
Download();
</script>
This way, your client will be asked whether to "Save" or "Run" the file.
Try this:
<html>
<body onload="myFunction()">
<script>
function myFunction() {
document.getElementById('a').click();
}
</script>
<a href="./image.pdf" download="image" id="a"> <img src="./image.pdf">
</a>
</body>
</html>