我遇到了几乎同样的问题,我需要在播放文件之前下载文件。我最终编写了这段代码,您可以轻松地为您的操作重写。
编辑:事实上,你几乎可以像使用代码一样,只是要小心下载的回调。
它利用回调来逐个下载每个文件(我遇到了带宽问题),但我有一个以前的版本,它开始所有下载,然后等待所有文件在回调之前都在磁盘上。
如果你想直接使用它,你需要一个名为 DOWNLOAD_DIR 的全局变量,其中包含下载目录的完整路径。
您还需要 http,但我认为您已经拥有它。
var http = require('http');
/*download
IN_: file_url
string
url of the file to download
callback
COM: Download the specified file to DOWNLOAD_DIR/name_of_the_file, and callback the full path to the file
callback null on error.
*/
function download(file_url, callback) {
var options = {
host: url.parse(file_url).host,
port: 80,
path: url.parse(file_url).pathname
},
file_name = url.parse(file_url).pathname.split('/').pop(),
//Creating the file
file = fs.createWriteStream(DOWNLOAD_DIR + file_name, {flags: 'w', encoding: 'binary'}),
console.log('Downloading file from ' + file_url);
console.log(LOG, '\tto ' + file_name);
http.get(options, function (res) {
res.pipe(file, {end: 'false'});
//When the file is complete
res.on('end', function () {
//Closing the file
file.end();
console.log(LOG, '\t\tDownloaded '+ file_name);
callback(DOWNLOAD_DIR + file_name);
});
});
process.on('uncaughtException', function(err) {
console.log('Can t download ' + file_url + '\t(' + err + ')');
callback(null);
});
}
/*download_all
IN_: list
array of string
Names of the files to download
callback
COM: Download all the file one after another
*/
function download_all(list, callback) {
var i = 0,
fe;
function follow() {
//If there is download to do
if (i < list.length) {
//Checking if the file already exist
fe = fs.existsSync(DOWNLOAD_DIR + list[i].substr(list[i].lastIndexOf('/')));
console.log('Checking ' + list[i]);
if (!fe) {
console.log('\tDo not exist');
//If it doesn t, downloading it
download(list[i], function () {
i = i + 1;
//And go to the next file
follow();
});
} else {
//If it does, go to the next file
console.log('\tExist');
i = i + 1;
follow();
}
} else {
//When all files are downloaded
console.log('end');
callback();
}
}
follow();
}
请注意,在生产代码中,您应该将 fs.existSync(下载中)替换为 fs.exist + 回调
编辑:这里是一次性下载的代码。请注意,这是我稍微编辑过的旧代码。
请注意,这段代码很旧,我没有对其进行大量测试,并且也使用了 fs.existSync(这同样不利于生产代码)。
最后说明,如果下载失败,下载的回调会有空有参数,你需要自己检查。
/*download_all
IN_: list
array of string
Names of the files to download
callback
COM: Download all-at-once
*/
function download_all(list, callback){
var i=0, dltd, dlcp=0;
dltd=list.length;
function afterDownload(){
dlcp=dlcp+1;
console.log("Telechargement fini:"+dlcp);
if(dlcp===dltd){
callback();
}
}
while(i<list.length)
{
if(!fs.existsSync(DOWNLOAD_DIR + list[i].substr(list[i].lastIndexOf('/'))))
{
//If the file do not exist
download(list[i], afterDownload);
} else {
afterDownload();
}
i=i+1;
}
}
例子:
var http = require('http'),
DOWNLOAD_DIR = '/home/user/download/',
list = ['http://somewebsite.com/video.mp4', 'http://somewebsite.com/video2.mp4', 'http://othersite.com/image.png'];
download_all(list, function (){
//Do stuff
});