等待文件完成复制后再执行操作



我有一个简单的node.js应用程序,它可以监视新PDF文件的目录。当它看到它们出现时,它会将它们FTP出来并移动到另一个目录。

我遇到的问题是,如果文件碰巧有点大(例如10MB),我的应用程序在完成复制到目录之前就开始处理该文件。

复制是通过网络进行的,所以速度也会慢一点。我需要一种方法来告诉我的应用程序等到文件完成复制后再处理它

最好的方法是什么?我试过"成长文件"模块,但它似乎不起作用,而且开发者似乎已经放弃了它

提前感谢您的帮助。

我遇到了几乎相同的问题,我需要在播放文件之前下载它们。我最终编写了这段代码,您可以很容易地为您的操作重写它。

编辑:事实上,你几乎可以像使用代码一样,只是要小心下载的回调。

它利用回调来逐个下载每个文件(我有带宽问题),但我有一个以前的版本,它启动了所有下载,然后等待所有文件在回调之前都在磁盘上。

如果您想将其用作全局变量,则需要有一个名为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, 'ttDownloaded '+ 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.extSync(同样,这对生产代码不好)。

最后要注意的是,如果下载失败,下载的回调将具有null参数,您必须自己检查。

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

最新更新