All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Download File On Button Click In JavaScript

Last Updated : Aug 29, 2023

Download File On Button Click In JavaScript

In this article we will show you the solution of download file on button click in JavaScript, as we see many web applications need to download files that’s why we are trying to make it possible for user to download file just by clicking a button.

We'll look at a quick and efficient JavaScript solution to complete this task.

We'll combine HTML, JavaScript, and a little imagination to get file downloads upon button click.

To handle the click event, JavaScript will be used to build an HTML button element.

By generating and clicking a hidden anchor element that is connected to the file that has to be downloaded, the JavaScript code will start the download.

Step By Step Guide On Download File On Button Click In JavaScript :-

<!DOCTYPE html>
<html>
<head>
  <title>File Download on Button Click</title>
</head>
<body>
  <button id="downloadButton">Download</button>
  <script>
    function downloadFile() {
      const fUrl = 'https://example.com/demo.txt';
      const fName = 'demo.txt';
      const link = document.createElement('a');
      link.href = fUrl;
      link.download = fName;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
    const button = document.getElementById('downloadButton');
    button.addEventListener('click', downloadFile);
  </script>
</body>
</html>
  1. For indicating HTML5 document, so that we declare HTML5 doctype (<!DOCTYPE html>) at the start point of the code.
  2. The HTML element is a part of HTML document from which code begins.
  3. The <head> element includes title and links to external resources like stylesheets and scripts.
  4. A <title> element with the wording "File Download on Button Click" is present inside the <head> element. This determines the title of the webpage that displays in the title bar or tab of the browser.
  5. When we clicked button A button element with the id property set to "downloadButton" can be found inside the body element. The action of downloading the file is started by pressing this button. When the button is clicked to begin the file download, the downloadFile function is invoked.
  6. A <script> element in HTML designates client-side JavaScript code.
  7. There is a JavaScript function named downloadFile() contained within the <script> tags. When the button is clicked, this function is in charge of starting the file download.
  8. The downloadFile function creates two constant variables: In the variable fUrl, the URL of the file that needs to be downloaded is stored. It is set in this instance to "https://example.com/sample_file.txt". The filename of the downloaded file is stored in the variable fName. It is set to 'sample_file.txt' in this illustration.
  9. Using document, a new <a> (anchor) element is generated programmatically.createElement('a'). Although it doesn't appear on the visible page, this element will be utilised to start the file download.
  10. The link.href property is set to fUrl, pointing to the file's location on the server.
  11. The filename of the file to be downloaded is specified by the value of the link.download property, which is set to fName.
  12. In the body as a child element the <a> element got added using document.body.appendChild(link). This make sure that the given link is present in document.
  13. The <a> element is promptly deleted from the document using document.body.removeChild(link) when the file download has been initiated. To keep the document tidy and remove any undesirable sections, this elimination is carried out.
  14. Using button, an event listener is attached to the button."click" and "downloadFile" in addEventListener. When the button is pressed, this event listener watches for the "click" event and calls the downloadFile function.

Conclusion :-

This article has successfully taught us how to use JavaScript to enable file downloads upon button click.

We can dynamically start the download process by creating a button element and attaching a click event listener.

This approach offers a simple, user-friendly solution to enable users to quickly download files from your web application.

Understanding this strategy can help you improve the functionality and user experience of your applications, which are crucial for many web projects.

I hope this article on download file on button click in JavaScript helps you and the steps and method mentioned above are easy to follow and implement