如何在Node.js的CLI中上传文件到文件夹

3
我正在尝试制作一个命令行界面,它可以仅上传特定扩展名的文件。例如,如果我想上传 .jpg 文件,则只能通过创建 JPG 文件夹来上传 JPG 文件。

const { program } = require("commander");

const fs = require("fs");
const path = require("path");

program.version("0.0.1");

program
  .command("file")
  .alias("f")
  .description("Add filename with filepath")
  .action(() => {
    prompt(questions).then((answers) => {
      try {
        // compare extension
        const extension = path.extname(answers.fileName);
        const allowedExtension = ".jpg";

        if (allowedExtension !== extension) {
          console.log("Use only .jpg Extension file");
        } else {
          // make dir
          fs.mkdir(path.join(__dirname, "JPG"), { recursive: true }, (err) => {
            if (err) {
              return console.error(err);
            }

            // read file or uploaded file
            const file = fs.createReadStream(
              `${answers.filePath}/${answers.fileName}`
            );
            console.log(
              "Directory created successfully!",
              answers.fileName,
              answers.filePath
            );
          });
        }
      } catch (error) {
        console.log(error.message);
      }
    });
  });

program.parse(process.argv);

但不知道如何使用CLI在提供的文件夹中上传文件。

2个回答

0

我从未使用过nodeJs,所以这可能有效也可能无效。你应该查找scp命令行传输两个源之间的内容,你可以使用npm安装node-scp模块(或者我读到的是这样)。

npm i node-scp

然后你需要开始导入它,定义源路径和目标路径,然后使用它。

var local_folder_path = './local/dir';
var detination_folder_path = '/server/path';

send_folder_using_async_await(local_folder_path, detination_folder_path);

async function send_folder_using_async_await(folder_path, 
destination_path)
{
   try {
       const client = await scp(remote_server)
       await client.uploadDir(folder_path, destination_path)
       client.close()
   } catch (e) {
      console.log(e)
   }
}

这样的东西。


0

使用writeFile函数上传它:

const { program } = require("commander");

const fs = require("fs");
const path = require("path");

program.version("0.0.1");

program
  .command("file")
  .alias("f")
  .description("Add filename with filepath")
  .action(() => {
    prompt(questions).then((answers) => {
      try {
        // compare extension
        const extension = path.extname(answers.fileName);
        const allowedExtension = ".jpg";

        if (allowedExtension !== extension) {
          console.log("Use only .jpg Extension file");
        } else {
          // make dir
          fs.mkdir(path.join(__dirname, "JPG"), { recursive: true }, (err) => {
            if (err) {
              return console.error(err);
            }

            const file = fs.createReadStream(
              `${answers.filePath}/${answers.fileName}`
            );
            fs.writeFile(`${answers.filePath}/${answers.fileName}`,file,(err) => console.log(err))
            console.log(
              "Directory created successfully!",
              answers.fileName,
              answers.filePath
            );
          });
        }
      } catch (error) {
        console.log(error.message);
      }
    });
  });

program.parse(process.argv);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接