Skip to content

Instantly share code, notes, and snippets.

@gkhays
Created February 27, 2020 17:26
Show Gist options
  • Save gkhays/fa9d112a3f9ee61c6005136ebda2a6fd to your computer and use it in GitHub Desktop.
Save gkhays/fa9d112a3f9ee61c6005136ebda2a6fd to your computer and use it in GitHub Desktop.
Download a file from a URL using Node.js

Download File from URL

Proof-of-concept to download a file from a URL and save it locally. For example, I may wish to retrieve a JAR file from Nexus.

Uses the http package, which does the basics with raw HTTP protocol support.

Possible update: use request, it is like the Python's requests library. There is a companion package called node-request-progress that tracks download progress. See request-progress on GitHub.

Note: The request package has been deprecated.

const http = require('http');
const fs = require('fs');

const file = fs.createWriteStream("file.jpg");
const request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) {
  response.pipe(file);
});

References

  1. How to download a file with Node.js (without using third-party libraries)?
  2. Get download progress in Node.js with request
  3. 5 Ways to Make HTTP Requests in Node.js
  4. What is the difference between request and http modules in node.js?
const http = require('http');
const fs = require('fs');
function getRemoteFile(file, url) {
let localFile = fs.createWriteStream(file);
const request = http.get(url, function(response) {
var len = parseInt(response.headers['content-length'], 10);
var cur = 0;
var total = len / 1048576; //1048576 - bytes in 1 Megabyte
response.on('data', function(chunk) {
cur += chunk.length;
showProgress(file, cur, len, total);
});
response.on('end', function() {
console.log("Download complete");
});
response.pipe(localFile);
});
}
function showProgress(file, cur, len, total) {
console.log("Downloading " + file + " - " + (100.0 * cur / len).toFixed(2)
+ "% (" + (cur / 1048576).toFixed(2) + " MB) of total size: "
+ total.toFixed(2) + " MB");
}
var targets = [
{
name: "remote-jar",
file: "jettison.jar",
url: "https://repo1.maven.org/maven2/org/codehaus/jettison/jettison/1.3.8/jettison-1.3.8.jar"
},
{
name: "remote-pom",
file: "jettison.pom",
url: "https://repo1.maven.org/maven2/org/codehaus/jettison/jettison/1.3.8/jettison-1.3.8.pom"
}
];
/*let file = 'jettison.jar';
let url = '';
getRemoteFile(file, url);*/
targets.forEach(function(item) {
getRemoteFile(item.file, item.url);
})
@jellehak
Copy link

This won't support https. should be something like this:

import http from 'http'
import https from 'https'
import fs from 'fs'

function getRemoteFile(file, url) {
    let localFile = fs.createWriteStream(file);
    const client = url.startsWith('https') ? https : http;
    const request = client.get(url, function(response) {
        var len = parseInt(response.headers['content-length'], 10);
        var cur = 0;
        var total = len / 1048576; //1048576 - bytes in 1 Megabyte

        response.on('data', function(chunk) {
            cur += chunk.length;
            showProgress(file, cur, len, total);
        });

        response.on('end', function() {
            console.log("Download complete");
        });

        response.pipe(localFile);
    });
}

function showProgress(file, cur, len, total) {
    console.log("Downloading " + file + " - " + (100.0 * cur / len).toFixed(2) 
        + "% (" + (cur / 1048576).toFixed(2) + " MB) of total size: " 
        + total.toFixed(2) + " MB");
}

var targets = [
    {
        name: "remote-jar",
        file: "jettison.jar",
        url: "https://repo1.maven.org/maven2/org/codehaus/jettison/jettison/1.3.8/jettison-1.3.8.jar"
    },
    {
        name: "remote-pom",
        file: "jettison.pom",
        url: "https://repo1.maven.org/maven2/org/codehaus/jettison/jettison/1.3.8/jettison-1.3.8.pom"
    }
];

/*let file = 'jettison.jar';
let url = '';
getRemoteFile(file, url);*/

targets.forEach(function(item) {
    getRemoteFile(item.file, item.url);
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment