我有一个从FTP服务器下载文件的方法,它可以在较小的文件上正常工作,但是当我使用它来下载~5GB大小的zip类型的文件时,它会下载它,但之后它不会做任何事情。当它达到下载的100%时,脚本不会继续。我是否应该等待下载完成后在后台实际执行某些操作?有文件大小限制吗?
const FTP = require('ftp')
可以在npm
找到downloadFile: params => {
return new Promise((resolve, reject) => {
let ftpClient = new FTP()
let total = params.state.fileSize
let progress = 0
ftpClient.on('ready', _ => {
console.log(`Downloading ${params.targetedFile} ...`);
ftpClient.get(params.targetedFile, (err, stream) => {
if (err) reject(err)
stream.on('data', buffer => {
progress += buffer.length
process.stdout.write(`Progress: ${(progress/total*100).toFixed(2)}% (${progress}/${total}) \r`)
})
stream.once('close', _ => {
ftpClient.end()
console.log(`Saved downloaded file to ${params.localDir}`);
resolve(params.localDir)
})
stream.pipe(fs.createWriteStream(params.localDir))
})
})
ftpClient.connect(params.auth)
})
}
基本上,下载大文件时,stream.once('close', ...)
的回调不会被执行。并且它会针对相同类型的较小文件执行。
答案 0 :(得分:0)
我建议您处理关闭写入流的事件。
原因很简单:我们读取ftp的读取流和管道来写入流,一切正常,文件成功关闭。
所以代码:
downloadFile: params => {
return new Promise((resolve, reject) => {
let ftpClient = new FTP()
let total = params.state.fileSize
let progress = 0
ftpClient.on('ready', _ => {
console.log(`Downloading ${params.targetedFile} ...`);
ftpClient.get(params.targetedFile, (err, stream) => {
if (err) {
ftpClient.end();
return reject(err);
}
stream.on('data', buffer => {
progress += buffer.length
process.stdout.write(`Progress: ${(progress/total*100).toFixed(2)}% (${progress}/${total}) \r`)
});
// opening writeStream to file
let finished = false;
const writeStream = fs.createWriteStream(params.localDir);
writeStream.on('finish', (result) => { // handling finish
finished = true;
ftpClient.end();
console.log(`Finish triggered ${params.localDir}`);
console.log(result);
resolve(params.localDir);
});
writeStream.on('close', (result) => { // handling close
ftpClient.end();
console.log(`Close triggered ${params.localDir}`);
console.log(result);
resolve(params.localDir);
})
// piping readStream to writeStream
stream.pipe(writeStream);
})
})
ftpClient.connect(params.auth)
})
}
答案 1 :(得分:0)
此代码可能会让您了解如何以一种黑客的方式处理此问题。
基本上,此方法允许您从FTP服务器下载文件并将其保存到本地文件系统。它在一行中输出当前进度complete_percentage% (current/total)
。完成后,它解析promise,返回本地文件的路径,与传递给param的那个文件相同。
/**
* @name downloadFile
* @desc downloads file from FTP server
* @param params, Object of params
* @prop auth: object, or null, authorization params
* @prop targetedFile: {String} filename e.g. data.txt
* @prop localDir: {String} filename on local disk
* @prop state: {Object} fileinfo object, {Int} .fileSize property is required
* @return Promise, resolves given localDir
*/
downloadFile: params => {
return new Promise((resolve, reject) => {
// validate param types
if(typeof params.auth !== 'object'
|| typeof params.targetedFile !== 'string'
|| typeof params.localDir !== 'string'
|| typeof params.state !== 'object'
|| typeof params.state.fileSize !== 'number'
) throw new Error('You are either missing properties or passed wrong types')
// initialize
let ftpClient = new FTP()
let total = params.state.fileSize
let progress = 0
//
ftpClient.on('ready', _ => {
console.log(`Downloading ${params.targetedFile} ...`)
// get file
ftpClient.get(params.targetedFile, (err, stream) => {
if (err){
ftpClient.end()
return reject(err)
}
// upon data receive
stream.on('data', buffer => {
progress += buffer.length
// if progress is complete
if(progress === total){
// start checking if local filesize matches server filesize
let interval = setInterval(_ => {
if(fs.statSync(params.localDir).size === total){
console.log(`Downloading file complete. Location: ${params.localDir}`);
clearInterval(interval)
ftpClient.end()
resolve(params.localDir)
}
})
}
// show current progress in percentages and bytes
process.stdout.write(`Progress: ${(progress/total*100).toFixed(2)}% (${progress}/${total}) \r`)
})
// pipe writestream to filesystem to write these bytes
stream.pipe(fs.createWriteStream(params.localDir))
})
})
ftpClient.connect(params.auth)
})//promise
}