当我在文件上使用readdir时,文件的大小是正确的。
使用FTP(JSFTP)对我的开发FTP服务器使用上面相同的技术工作得很好。
醇>
任何建议表示赞赏!
这是我的代码:
var Client = require('ssh2').Client;
var m_ssh2Credentials = {
host: config.ftpHostName,
port: config.ftpPort,
username: config.ftpUser,
password: config.ftpPassword,
readyTimeout: 20000,
algorithms: { cipher: ["3des-cbc", "aes256-cbc", "aes192-cbc","aes128-cbc"]}
};
...
var conn = new Client();
var dataLength = 0;
conn.on('ready', function() {
conn.sftp(function(err, sftp) {
if (err) {
writeToErrorLog("downloadFile(): Failed to open SFTP connection.");
} else {
writeToLog("downloadFile(): Opened SFTP connection.");
}
var streamErr = "";
var dataLength = 0;
var stream = sftp.createReadStream(config.ftpPath + "/" + m_fileName)
stream.on('data', function(d){
data.push(d);
dataLength += d.length;
});
.on('error', function(e){
streamErr = e;
})
.on('close', function(){
if(streamErr) {
writeToErrorLog("downloadFile(): Error retrieving the file: " + streamErr);
} else {
writeToLog("downloadFile(): No error using read stream.");
m_fileBuffer = Buffer.concat(data, dataLength);
writeToLog("Data length: " + dataLength);
writeToLog("downloadFile(): File saved to buffer.");
}
conn.end();
});
})
})
.on('error', function(err) {
writeToErrorLog("downloadFile(): Error connecting: " + err);
}).connect(m_ssh2Credentials);