So, I've been looking and trying to find a way to download a file automatically right when somebody goes onto my site. I've tried using an a tag to download, and it works, you just have to click to download it. Like so...
<a href="pic.jpg" download>Download</a>
But I don't want that. I want it to automatically download with no click. I need some help please!
Another way to do it :
var a = document.createElement('a');
a.setAttribute('href', dataUri);
a.setAttribute('download', filename);
var aj = $(a);
aj.appendTo('body');
aj[0].click();
aj.remove();
Another option which is pure javascript is:
const downloadFile = (file) => {
const element = document.createElement('a');
element.setAttribute('href', 'Download Btn');
element.setAttribute('download', file);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
And then you can call the function on load:
downloadFile(/*pass your file here*/);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With