Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto download file from a link using javascript

Tags:

javascript

How can I download a file in a link (say: https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png) using javascript?

Edit: I want to download it automatically without any user interaction. There may be a lots of file. And all of them need to be downloaded on page load.

like image 570
Shafiq Avatar asked Sep 15 '25 22:09

Shafiq


1 Answers

You can use the HTML5 download attribute, and do it without javascript

<a href="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" download>download file</a>

To make it run without user interaction, you can create the anchor and trigger a click on it with javascript

var a = document.createElement('a');
a.href = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png";
a.download = 'download';

a.click();
like image 134
adeneo Avatar answered Sep 17 '25 11:09

adeneo