Nodejs jsftp 求指导 ,求给一个demo ,有没有谁写出来文件夹上传下载功能,就是整个目录的上传下载

Nodejs jsftp 求指导 ,求给一个demo ,有没有谁写出来文件夹上传下载功能,就是整个目录的上传下载

楼主有没有解决下载整个目录的问题 求分享

2 回复

Node.js 使用 jsftp 实现文件夹上传和下载功能

背景

在 Node.js 中使用 jsftp 库来实现 FTP 的基本操作是相对简单的。然而,实现文件夹的上传和下载需要一些额外的逻辑来处理文件和子文件夹。

安装 jsftp

首先确保你已经安装了 jsftp 库。如果没有安装,可以通过 npm 进行安装:

npm install jsftp

文件夹上传示例

以下是一个简单的示例代码,演示如何使用 jsftp 上传一个目录及其所有子文件和子目录到 FTP 服务器。

const JSFtp = require('jsftp');
const fs = require('fs');
const path = require('path');

const Ftp = new JSFtp({
    host: "your_ftp_host",
    port: 21,
    user: "your_username",
    pass: "your_password"
});

async function uploadDirectory(localDir, remoteDir) {
    const files = await fs.promises.readdir(localDir);

    for (let file of files) {
        const localFilePath = path.join(localDir, file);
        const isDirectory = (await fs.promises.stat(localFilePath)).isDirectory();

        if (isDirectory) {
            await Ftp.mkdir(`${remoteDir}/${file}`, true); // 创建远程目录
            await uploadDirectory(localFilePath, `${remoteDir}/${file}`); // 递归上传子目录
        } else {
            await Ftp.put(fs.createReadStream(localFilePath), `${remoteDir}/${file}`); // 上传文件
        }
    }
}

uploadDirectory('/local/path/to/directory', '/remote/path/to/directory')
    .then(() => console.log('Upload completed'))
    .catch(err => console.error('Error during upload:', err));

文件夹下载示例

接下来是如何从 FTP 服务器下载一个完整的目录结构。

async function downloadDirectory(remoteDir, localDir) {
    const files = await Ftp.list(remoteDir);

    for (let file of files) {
        const remoteFilePath = `${remoteDir}/${file.name}`;
        const localFilePath = path.join(localDir, file.name);
        const isDirectory = file.type === 'd'; // 判断是否为目录

        if (isDirectory) {
            await fs.promises.mkdir(localFilePath, { recursive: true }); // 创建本地目录
            await downloadDirectory(remoteFilePath, localFilePath); // 递归下载子目录
        } else {
            await Ftp.get(remoteFilePath, fs.createWriteStream(localFilePath)); // 下载文件
        }
    }
}

downloadDirectory('/remote/path/to/directory', '/local/path/to/directory')
    .then(() => console.log('Download completed'))
    .catch(err => console.error('Error during download:', err));

总结

以上代码展示了如何使用 jsftp 实现文件夹的上传和下载。通过递归调用自身,可以轻松地处理多层嵌套的目录结构。希望这些示例对你有所帮助!


针对你的需求,你可以使用 jsftp 库来实现 Node.js 下的 FTP 文件夹上传和下载。这里我会给你提供一个简单的示例代码来完成这一功能。

首先,确保你已经安装了 jsftp

npm install jsftp

接下来是上传整个文件夹到 FTP 的代码示例:

const JSFtp = require('jsftp');
const fs = require('fs');
const path = require('path');

const FtpConfig = {
    host: "your_ftp_host",
    port: 21,
    user: "your_username",
    pass: "your_password"
};

const localDirPath = './local/path'; // 本地需要上传的文件夹路径
const remoteDirPath = '/remote/path'; // 远程服务器上存放的文件夹路径

const JFtp = new JSFtp(FtpConfig);

async function uploadDirectory(localPath, remotePath) {
    const files = await fs.promises.readdir(localPath);
    for (const file of files) {
        const localFilePath = path.join(localPath, file);
        const stats = await fs.promises.stat(localFilePath);
        if (stats.isDirectory()) {
            JFtp.mkdir(remotePath + '/' + file, () => {
                uploadDirectory(localFilePath, remotePath + '/' + file);
            });
        } else {
            JFtp.put(fs.createReadStream(localFilePath), remotePath + '/' + file, (err) => {
                if (err) console.log(err);
            });
        }
    }
}

uploadDirectory(localDirPath, remoteDirPath).then(() => {
    console.log('Upload complete!');
}).catch((err) => {
    console.error('Error:', err);
});

对于下载整个目录的功能,可以这样实现:

async function downloadDirectory(localPath, remotePath) {
    JFtp.list(remotePath, (err, res) => {
        if (err) throw err;
        
        for (const file of res) {
            const remoteFilePath = remotePath + '/' + file.name;
            const localFilePath = path.join(localPath, file.name);

            if (file.type === 'd') {
                fs.mkdirSync(localFilePath, { recursive: true });
                downloadDirectory(localFilePath, remoteFilePath);
            } else {
                JFtp.get(remoteFilePath, fs.createWriteStream(localFilePath), (err) => {
                    if (err) console.log(err);
                });
            }
        }
    });
}

以上代码应该能满足你的需求,但请注意这些代码片段没有经过实际测试,你可能需要根据实际情况进行一些调整。希望这能帮助到你!

回到顶部