admin管理员组文章数量:1290949
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= '.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 pletes.
});
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.
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./archive/sounds/ic/ic002.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 pletes.
});
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.
Share Improve this question edited May 5, 2017 at 19:00 NathanOliver 181k29 gold badges315 silver badges430 bronze badges asked May 5, 2017 at 17:27 RajasekarRajasekar 31 gold badge1 silver badge3 bronze badges 4-
It doesn't look like you actually call the
download
function, so nothing is happening. – Karl Reid Commented May 5, 2017 at 17:31 -
Why do you have
'
character before var and then the last line}'
? – bhantol Commented May 5, 2017 at 17:45 - None of the answers will work until those quotes are fixed. – bhantol Commented May 5, 2017 at 17:47
- Possible duplicate of How to download a file with Node.js (without using third-party libraries)? – Gabe Rogan Commented May 5, 2017 at 18:31
4 Answers
Reset to default 6Duplicate 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./archive/sounds/ic/ic002.wav", function(response) {
response.pipe(file);
});
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 plete");
}
});
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
.
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);
})
})
}
This should work, I tested this with several sources from freesound, e.g. this one: https://cdn.freesound/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,
});
}
本文标签: javascriptHow to download audio file from URL in NodejsStack Overflow
版权声明:本文标题:javascript - How to download audio file from URL in Node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741524283a2383375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论