Reputation: 3
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.
Upvotes: 0
Views: 19869
Reputation: 149
This should work, I tested this with several sources from freesound.org, e.g. this one: https://cdn.freesound.org/previews/512/512246_7704891-hq.mp3
public static async downloadAudio(audioSource: string, path: string) {
const response = await axios.get(audioSource, {
responseType: 'arraybuffer',
});
const arrayBuffer = response.data;
const uintArray = new Uint8Array(arrayBuffer);
const byteArray = Array.from(uintArray);
const base64String = btoa(byteArray.map((char) => String.fromCharCode(char)).join(''));
// choose an appropriate file writer here
await Filesystem.writeFile({
path: path,
data: base64String,
directory: Directory.Documents,
recursive: true,
});
}
Upvotes: 0
Reputation: 1
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);
})
})
}
Upvotes: 0
Reputation: 2217
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
.
Upvotes: 0
Reputation: 3711
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);
});
Upvotes: 6