0

How to trigger a file download when clicking an HTML button or JavaScript?

<a href="geeksforgeeks.png" download="GFG">
  • 1
    Welcome to Stack Overflow. Stack Overflow is the place to ask questions about problems with _your code_. It's _not_ the place to ask for a tutorial or some free programmer to do your job for you. Please check out [How to ask a good question](https://stackoverflow.com/help/how-to-ask). Then edit your question with what you've tried yourself and where you're running into issues. – icecub Jul 23 '22 at 09:15
  • [W3C create Download Link](https://www.w3schools.com/howto/howto_html_download_link.asp) i think you missing – Phương Nguyễn Jul 23 '22 at 09:50

1 Answers1

-1
var link = document.createElement('a');
var file_name = $('.download_btn').attr('href');
link.href = file_name;
var filename = file_name.split('/').pop();
link.download = filename;
link.dispatchEvent(new MouseEvent('click'));
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Jul 23 '22 at 15:08