node ftp上传文件夹

ftp包中未找到直接上传文件夹的方法,basic-ftp包可以直接上传文件夹

basic-ftp文档:basic-ftp - npm (npmjs.com)

1、安装basic-ftp

npm install basic-ftp -D

2、项目根目录创建up.js文件

3、示例代码

// 将打包后dist上传ftp
const ftp = require('basic-ftp')
const path = require('path')

// 上传
up()

async function up() {
  const client = new ftp.Client()
  try {
    await client.access({
      host: '***.***.***.***',
      user: '用户名',
      password: '密码'
    })

    // 上传进度
    client.trackProgress(info => {
      if (info.bytes) {
        console.log(
          `上传:${info.name};大小:${
            info.bytes > 1024
              ? info.bytes > 1024 * 1024
                ? (info.bytes / 1024 / 1024).toFixed(2) + 'M'
                : (info.bytes / 1024).toFixed(2) + 'K'
              : info.bytes + 'B'
          }`
        )
        console.log('========================================')
      }
    })

    // 本地dist打包路径
    const dist_path = path.resolve(process.cwd(), './dist')

    // 远端ftp路径
    const ftp_path = '/www/xxxx'

    // 进入远端目录,没有则创建,设当前目录为工作区
    await client.ensureDir(ftp_path)
    // 清空工作区文件
    await client.clearWorkingDir()
    // 上传
    await client.uploadFromDir(dist_path, ftp_path)
  } catch (err) {
    console.log('上传失败:', err)
  }
  // 关闭
  client.close()
}

4、执行命令

node up
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容