node ftp上传

const fs = require('fs');
const FTPClient = require('ftp');
const path = require('path')

const dirPath = path.join(__dirname, './dist'); // 本地FTP目录
const remoteFtpPath = '/t1/star-pc-upload/test'; // 远程FTP目录
const localFiles = []; // 本地文件
let localFileLength = 0; // 上传文件个数
let uploadTime = 0; // 上传耗时
let ftp = null;

readingFile().then(()=>{
    // console.log(localFiles);
    if(localFileLength) {
        // 上传文件 - 覆盖
        console.log('连接10.41.41.168...');
        ftp = new FTPClient();
        ftp.on('ready', () => {
            // 清理文件
            console.log('连接成功,清理旧文件...');
            ftp.rmdir(remoteFtpPath,true,err=>{
                if(err) console.log('目录不存在,无需清理');
                console.log('开始上传...');
                uploadingFile().then(()=>{
                    console.log(`上传结束,本次上传${localFiles.length}个文件`);
                }).catch(err=>{
                    console.log(err);
                })
            });
        });
        // 连接服务器
        ftp.connect({
            host:'服务器IP',
            port:21,
            user:'用户名',
            password:'密码',
            connTimeout: 1000*10, // 连接超时时间
            pasvTimeout: 1000*10, // PASV data 连接超时时间
            keepalive: 1000* 10, // 多久发送一次请求,以保持连接
        });
    } else {
        console.log('本地文件为空,取消上传');
    }
}).catch(err=>{
    console.log(err);
});

// 正在获取本地文件
function readingFile() {
    return new Promise((resolve,reject)=>{
        readFiles(dirPath).catch(err=>reject(err));
        // 判断是否读取完毕(1s内 localFiles长度不再变化)
        console.log('正在读取本地文件,请稍后...');
        let timer = setInterval(() => {
            if(localFileLength == localFiles.length) {
                clearInterval(timer);
                resolve();
                return console.log('读取完成');
            }
        }, 1000);
    })
}
// 递归读取文件
function readFiles(filepath) {
    return new Promise((resolve,reject)=>{
        fs.readdir(filepath,{withFileTypes:true},(err,files)=>{
            if(err) throw err;
            if(files.length>0) {
                files.map(file=>{
                    if(file.isFile()) { // 文件
                        const child_filepath = filepath + '\\' + file.name;
                        fs.readFile(child_filepath,(err,data)=>{
                            if(err) throw err;
                            const dir = remoteFtpPath + filepath.replace(dirPath,'').replace('\\','/') ;
                            localFiles.push({
                                dir,
                                filepath: dir + '/' +file.name,
                                filedata: data
                            })
                            localFileLength = localFiles.length;
                        });
                    } else { // 目录
                        const child_filepath = filepath + '\\' + file.name;
                        readFiles(child_filepath);
                    }
                });
            }
        });
    });
}

// 正在上传文件
function uploadingFile() {
    return new Promise((resolve,reject)=>{
        localFiles.map(file=>{
            uploadFiles(file.dir,file.filepath,file.filedata).catch(err=>reject(err));
        })
        console.log('正在上传,请稍后...');
        let timer = setInterval(() => {
            if(localFileLength == 0) {
                clearInterval(timer);
                resolve();
                return console.log('上传完成');
            }
            uploadTime++;
            console.log(`已用时:${uploadTime}s`);
        }, 1000);
    })
}
// 覆盖上传文件
function uploadFiles(dir,filepath,filedata) {
    return new Promise((resolve,reject)=>{
        ftp.mkdir(dir,true,err1=>{
            if(err1) reject(err1);
            ftp.put(filedata,filepath,err2 => {
                if (err2) reject(err2);
                localFileLength--;
                ftp.end();
                resolve();
            });
        })
    })
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值