Javascript download file from server

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Suppose you want to download a file when you click on a link. For downloading the file, we mentioned here to implementation as well as folder structure where you can see the file location.

    Approach:

    • Create an anchor tag link on the normal HTML page. We want to download a file when we click on an anchor tag link(Download this file).

    html

    <!DOCTYPE html>

    <html>

    <head>

        <title>

            Download File Using 

            JavaScript/jQuery

        </title>

    </head>

    <body>

        <h2>

            Download File Using 

            JavaScript/jQuery

        </h2>

        <a id="link" href="#">

            Download this file

        </a>

    </body>

    </html>

    • Provide with the below JavaScript Code:
    $(document).ready(function () {
          $("#link").click(function (e) {
              e.preventDefault();
              window.location.href = "File/randomfile.docx";
          });
    });
    
    // Note: url= your file path
    
    • Note: Replace the above URL with your file path. 
      Implementation and Folder structure is as shown below.

    Javascript download file from server

    Example:

    html

    <!DOCTYPE html>

    <html>

    <head>

        <script src=

        </script>

    </head>

    <body>

        <h2>

            Download File Using 

            JavaScript/jQuery

        </h2>

        <h3>

            For Downloading, Click 

            on the below link.

        </h3>

        <a id="link" href="no-script.html">

            Download this file

        </a>

        <script>

            $(document).ready(function () {

                $("#link").click(function (e) {

                    e.preventDefault();

                    window.location.href 

                        = "File/randomfile.docx";

                });

            });

        </script>

    </body>

    </html>

    Output:

    https://media.geeksforgeeks.org/wp-content/uploads/20201008125653/Screencast-from-08-10-20-124101-PM-IST.webm

    jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.
    You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.


    How do I download a file using JavaScript?

    So the steps for downloading the file will be:.
    Use fetch API to download the script file..
    Transform the data to a blob type..
    Convert the blob object to a string by using URL. createObjectURL() ..
    Create an <a> element to download the string..

    How do I download a file from a server?

    To transfer a file from a remote server via SSH using SCP, we need the following pieces of information:.
    Login credentials – username, server name or IP address, and password..
    The port number for SSH connections..
    The path to the file on the remote server..
    The path to the download location..

    How do I download a file from a node js server?

    Method 1: Using 'https' and 'fs' module We can use the http GET method to fetch the files that are to be downloaded. The createWriteStream() method from fs module creates a writable stream and receives the argument with the location of the file where it needs to be saved.

    How download all JavaScript file from website?

    open the Js script link and press ctrl+s i.e save it. Save it by any desired name and then copy it your project folder and then include it in your project files where you have included other files like jquery and css. Thanks first for your help!. When I press ctrl + s its save the all page.