gpt4 book ai didi

javascript - 如何从 Node.js 中的 URL 下载音频文件?

转载 作者:行者123 更新时间:2023-12-01 03:42:00 32 4
gpt4 key购买 nike

如何从URL下载音频文件并将其存储在本地目录中?我正在使用 Node.js,并尝试了以下代码:

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);
});
});
}

没有发生错误,但尚未找到文件。

最佳答案

How to download a file with Node.js (without using third-party libraries)? 的重复项,但这是特定于您的问题的代码:

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);
});

关于javascript - 如何从 Node.js 中的 URL 下载音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43810700/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com