0

How to download audio file from URL and store it in local directory? I'm using Node.js and I tried the following code:

var http = require('http');
var fs = require('fs');
var dest = 'C./test'
var url= 'http://static1.grsites.com/archive/sounds/comic/comic002.wav'
function download(url, dest, callback) {
  var file = fs.createWriteStream(dest);
  var request = http.get(url, function (response) {
    response.pipe(file);
    file.on('finish', function () {
      file.close(callback); // close() is async, call callback after close completes.
    });
    file.on('error', function (err) {
      fs.unlink(dest); // Delete the file async. (But we don't check the result)
      if (callback)
        callback(err.message);
    });
  });
}

No error occured but the file has not been found.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Rajasekar
  • 3
  • 1
  • 1
  • 3
  • It doesn't look like you actually call the `download` function, so nothing is happening. – Karl Reid May 05 '17 at 17:31
  • Why do you have `'` character before var and then the last line `}'` ? – bhantol May 05 '17 at 17:45
  • None of the answers will work until those quotes are fixed. – bhantol May 05 '17 at 17:47
  • Possible duplicate of [How to download a file with Node.js (without using third-party libraries)?](http://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js-without-using-third-party-libraries) – Gabe Rogan May 05 '17 at 18:31

3 Answers3

6

Duplicate of How to download a file with Node.js (without using third-party libraries)?, but here is the code specific to your question:

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

var file = fs.createWriteStream("file.wav");
var request = http.get("http://static1.grsites.com/archive/sounds/comic/comic002.wav", function(response) {
  response.pipe(file);
});
Community
  • 1
  • 1
Gabe Rogan
  • 3,343
  • 1
  • 16
  • 22
  • @RajaSekar You're welcome! Please mark the answer as correct so other people know the question has been answered. :) – Gabe Rogan May 05 '17 at 18:18
  • 1
    does `fs.createWriteStream("file.wav")` create a file on the server? or does it just make it available to send back to client? – user1063287 Jul 14 '19 at 03:45
  • @user1063287 no, `fs.createWriteStream("file.wav")` does not create a file on the server. it opens a write stream to a file, which means that as bytes slowly come in from somewhere (the server), we have told the computer to let these bytes trickle into the hard drive _as they arrive_ (file.wav). – Gabe Rogan Jul 15 '19 at 10:05
0

Your code is actually fine, you just don't call the download function. Try adding this to the end :

download(url, dest, function(err){
   if(err){
     console.error(err);
   }else{
     console.log("Download complete");
   }
});

Also, change the value of dest to something else, like just "test.wav" or something. 'C./test' is a bad path.

I tried it on my machine and your code works fine just adding the call and changing dest.

Karl Reid
  • 2,147
  • 1
  • 10
  • 16
0

Here is an example using Axios with an API that may require authorization

const Fs = require('fs');
const Path = require('path');
const Axios = require('axios');

async function download(url) {
    let filename = "filename";
    const username = "user";
    const password = "password"
    const key = Buffer.from(username + ':' + password).toString("base64");
    const path = Path.resolve(__dirname, "audio", filename)
    const response = await Axios({
        method: 'GET',
        url: url,
        responseType: 'stream',
        headers: { 'Authorization': 'Basic ' + key }
    })
    response.data.pipe(Fs.createWriteStream(path))
    return new Promise((resolve, reject) => {
        response.data.on('end', () => {
            resolve();
        })
        response.data.on('error', () => {
            reject(err);
        })
    })
}